Android: bug in launchMode="singleTask"? -> activity stack not preserved

znq picture znq · Mar 10, 2010 · Viewed 37k times · Source

My main activity A has as set android:launchMode="singleTask" in the manifest. Now, whenever I start another activity from there, e.g. B and press the HOME BUTTON on the phone to return to the home screen and then again go back to my app, either via pressing the app's button or pressing the HOME BUTTONlong to show my most recent apps it doesn't preserve my activity stack and returns straight to A instead of the expected activity B.

Here the two behaviors:

Expected: A > B > HOME > B
Actual: A > B > HOME > A (bad!)

Is there a setting I'm missing or is this a bug? If the latter, is there a workaround for this until the bug is fixed?

FYI: This question has already been discussed here. However, it doesn't seem that there is any real solution to this, yet.

Answer

renchenyu picture renchenyu · Jun 9, 2010

This is not a bug. When an existing singleTask activity is launched, all other activities above it in the stack will be destroyed.

When you press HOME and launch the activity again, ActivityManger calls an intent

{act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER]flag=FLAG_ACTIVITY_NEW_TASK|FLAG_ACTIVITY_RESET_IF_NEEDED cmp=A}

So the result is A > B > HOME > A.

It's different when A's launchMode is "Standard". The task which contains A will come to the foreground and keep the state the same as before.

You can create a "Standard" activity eg. C as the launcher and startActivity(A) in the onCreate method of C

OR

Just remove the launchMode="singleTask" and set FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP flag whenever call an intent to A