Suppose an application already has a stack of activities, and then a new activity is started from the application context with FLAG_ACTIVITY_NEW_TASK. And this happens within the same process, i.e. inside the application, like this:
AppContext -> A(FLAG_ACTIVITY_NEW_TASK) -> B -> C; AppContext -> D(FLAG_ACTIVITY_NEW_TASK)
According to the documentation we have 2 tasks now:
Task 1: A, B, C; Task 2: D
It follows from the doc that it is theoretically possible for the user to switch either to activity C or to activity D independently, as they belong to different tasks. In practice, however, I wasn't able to switch to activity C - it always switched to D.
My question is: Is it true that, if started in the same process with FLAG_ACTIVITY_NEW_TASK, tasks are not independent and the user can only switch to the topmost activity in the topmost task?
If the answer is "no" then what is the way for the user to switch to task 1?
The answer is: No
The tasks are independent and the user can switch between their activities independently, effectively having two activities of your app on the top of the stack. I just found the use case, three years after asking the original question.
Our app registers itself as a handler for a URL scheme (like market://xxx.yyy/zzz
). Now let's imagine the user started the app with the launcher, got into a middle of something (activities A, B, C
), and left off. Then in the browser the user clicks on a URL so our other activity is launched (activity D
). This results in having two tasks in the same time: activities A, B, C
(with C
on top) and activity D
. The user can switch between these two activities via task history.
Funny that Activity C is visible in the task history with the app's own icon but the Activity D is visible with the browser's icon. Regardless, both can be switched to.
Also, both tasks A-B-C
and D
run in the same process. This means that all singletone objects and static data are shared between them. As a result, they may corrupt each other's state if caution is not taken.
It's been a while after I've asked this question when I came across this link which I find very useful. It describes all flags and combinations very clear.
http://blog.akquinet.de/2010/04/15/android-activites-and-tasks-series-intent-flags/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With