views:

137

answers:

3

we start activity and do not calling finish() on existing activity it keeps existing activity in stack and move to new activity if we press back button we return to previous activity.

Its mean all previous activities not beeing called finish() keep remain into the memory(Stack)

Now problem is i do not want to call finish for every activity is there any centralize place where i can define that keep only last 3 activities in stack and remove rest of them ?

+5  A: 

In the Android Manifest in the [activity] tag you can specify android:noHistory - Whether or not the activity should be removed from the activity stack and finished (its finish() method called) when the user navigates away from it and it's no longer visible on screen —"true" if it should be finished, and "false" if not. The default value is "false". A value of "true" means that the activity will not leave a historical trace. It will not remain in the activity stack for the task, so the user will not be able to return to it. This attribute was introduced in API Level 3.

fupsduck
+1This can be thought as equivalent of calling finish() in onStop() of your activity. You can also make use of Intent Flags to control Activities on the stack.
Samuh
+1  A: 

It sounds like you're worried about memory usage and you shouldn't be: Android handles all this for you.

When one of your Activities covered by another of your Activities so it is no longed visibile to the user it is stopped. A stopped Activity should still retain all its state but can be killed by the system when memory is needed. This is why you need to implement the onStop() and onRestart() methods so that your Activity can recover its state after being killed and restarted automatically by the system.

For more detail you can read about Component Lifecycles in the Androind Fundamentals document in the Android Developer documentation.

Dave Webb
+1  A: 

When launching new Intents that resolve to Activities, consider setting appropriate Intent Flags, list of such flags can be found here. Using these flags, you can re-order activities on the history stack and clear it too. There is an excellent description of this in the Activity and Task guideline documentation see here.

Alternatively, in your deployment descriptor also known as Android Manifest, set appropriate attributes for your activity(noHistory would be a fit for your problem). Refer this for details.

Samuh