Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I clear all Activities from the stack?

I am having trouble popping all activities off the stack using Intent.FLAG_ACTIVITY_CLEAR_TOP and android:launchMode="singleInstance".

In my application activity A, launches activity B (via startActivity) which in turn launches activity C (via startActivity). On activity C the user presses a menu item to return to activity A. When they arrive at activity A, I want only A on the stack such that if they click the back button they return to the home screen (desktop).

This is the code that I am currently using when the user presses a button to return to A:

Intent i = new Intent(this, A.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);

All activities are defined as android:launchMode="singleInstance" in the project manifest.

My code doesn't seem to work though. Once I'm back on activity A if I click the back button I return to activity C. Am I misunderstanding how to use Intent.FLAG_ACTIVITY_CLEAR_TOP?

like image 985
Richard Avatar asked Jul 08 '10 04:07

Richard


1 Answers

I've always found the best way to ensure C would be removed from the stack is to call finish() after startActivity to remove C from the stack.

The documentation does read as though things would behave the way you expected them to, but it would seem this isn't happening, so finish() will ensure C is removed.

like image 108
Al Sutton Avatar answered Oct 27 '22 03:10

Al Sutton