Reputation: 81
I have a list of activities A - B -C -D - E and more, for example final activity is K. I want clear all these activities in stack when i press BACK button. How can i do ? In fact, i over ride
onBackPress(){
moveTaskToBack(true);
finish();
}
but only current activity is deleted and application exit. Then, i come back application, it resume activity before K. I want it start from begining when i re-open app. I think the reason here is because the list of activities in stack still are stored, so i want to clear all stack when clicking BACK button. Any suggestions ? thank you very much !
Upvotes: 4
Views: 6028
Reputation: 22945
In API level 11 or greater, use FLAG_ACTIVITY_CLEAR_TASK
and FLAG_ACTIVITY_NEW_TASK
flag on Intent to clear all the activity stack.
Add this code on your onBackPressed()
method,
> This launch mode can also be used to
good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. This is especially useful, for example, when launching an activity from the notification manager.
So your code to launch B
would be:
Intent intent = new Intent(A.this, B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish(); // call this to finish the current activity
Upvotes: 1
Reputation: 111
There is method called finishAffinity() for finishing all activity.
public void onBackPressed(){
super.onBackPressed();
this.finishAffinity();}
Upvotes: 6
Reputation: 1468
You need to call your activity with the FLAG_ACTIVITY_CLEAR_TOP
inside your onBackPressed
@Override
public void onBackPressed()
{
Intent it = new Intent(YouCurrentActivity.this, YourFinalActivity.class);
it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(it);
finish();
}
Hope it Helps!
Upvotes: 5
Reputation: 10977
Either use the noHistory
flag in the manifest or finish each activity yourself when the user navigates away.
startActivity(myIntent);
finish();
Another solution, maybe the best, if you have so many overlaying Activities
: use only one Activity
and handle the content in Fragments
. This way you are in control what exactly you want to show when the user hits the back button.
Upvotes: 2