We know the influence of SingleTask mode on Activity is as follows:
For example, if we set Activity A to SingleTask mode, and other Acitivity to Standard mode, there are the following effects:
A -> B -> C -> D We will jump Activity A to Activity D. If D -> A at this time, Activity B will happen, C, D will be popped out of Activity A then Will display to the top of the stack.
When I use this feature, I handle single-ended login: Set Activity A to SingleTask mode and set it to activated Acitivity,
If the background tells the account to log in elsewhere , Then you can jump to Activity A directly at this time, and other Acitivity will be popped out.
Now the problem is coming. When we set the MainActivity of the App to SingleTask mode, if we press the Home button when the App jumps to Activity B and click on the App Icon at this time,
p>
At this time, Activity B is not displayed, or Activity A will be displayed directly, that is, the Activity above A will be popped out, and Activity A will be displayed to the top of the stack, but this is not what I want.
Problem description:
My main activity A
has as set android:launchMode="singleTask"
in the manifest. Now , whenever I start another activity from there, eg 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 BUTTON
long 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!)
How to deal with it:
Just remove the launchMode="singleTask"
and set FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP
flag whenever call an intent to A
Link: https://stackoverflow.com/questions/2417468/android-bug -in-launchmode-singletask-activity-stack-not-preserved