views:

57

answers:

2

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?

+1  A: 

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.

Al Sutton
Calling finish() would remove C, but B would still be on the stack. I want A, B, C all in the stack until the user specifically requests to go from C -> A.
Richard
Al Sutton
A: 

I usually use the technique Al suggested (calling finish() after starting the new activity).

You could also experiment with task affinity. I've never done that myself, but it may be relevant in your case as well. See this thread: http://groups.google.com/group/android-developers/browse_frm/thread/ca3b26a14d024597/129e37375105901b

EboMike