I have 5 activity in back stack. At a time, I want to remove 4 child activity from back stack. how can I do this? i dont want to finish each activity by hand. is there any method which can make my back stack empty?
Asked
Active
Viewed 4,148 times
2
-
Have a look at this thread - http://stackoverflow.com/questions/1898886/removing-an-activity-from-the-history-stack – KV Prajapati Jul 29 '11 at 06:49
2 Answers
3
You can start your fifth activity with the FLAG_ACTIVITY_CLEAR_TOP
flag.

iDroid
- 10,403
- 1
- 19
- 27
-
the flow of activity is A -> B -> C -> A -> B -> C -> A . whenever i come back to activity A i want to finish all other activity. is FLAG_ACTIVITY_CLEAR_TOP work? if yes then please let me know the syntax. thanks – Jul 29 '11 at 08:26
-
Assuming you are in Activity C, if you now start Activity A like this: `Intent intent = new Intent(this, ActivityA.class) .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent);` Then all activities but A are killed as soon as A is started. – iDroid Jul 29 '11 at 08:54
-
-
Afaik there is no method like `clearActivityStack()` to clear the whole stack from a certain activity, if you mean that. You can wether clear the history on launching a new activity with the flag like already mentioned or you can end the activity manually by calling `finish()`. Also take a look at [this](http://developer.android.com/reference/android/content/Intent.html) reference, it countains all available flags and methods for handling activities. – iDroid Jul 29 '11 at 09:37
0
What you want is essentially being able to end application from any other activity different from the first one.
What I do is using an application variable determining if application must be shutdown and then check it on the onresume method.
protected void onResume()
{
super.onResume();
if (!getMyApplication().isPlatformActive())
{
/* We finish every activity after resuming if we must shutdown application */
finish();
}
}
boolean isRootScreen()
{
/* Every activity will override this and set it to true if want to exit application from here */
return false;
}
protected void onBackPressed()
{
if(isRootScreen())
{
/* You could show a confirmation dialog here */
getMyApplication().setPlatformActive(false);
}
super.onBackPressed();
}

Fernando Miguélez
- 11,196
- 6
- 36
- 54