I've been reading doc regarding to launch modes and there is one thing I don't understard. The doc says that singleTask activity is always the root of the stack:
In contrast, "singleTask" and "singleInstance" activities can only
begin a task. They are always at the root of the activity stack.
Moreover, the device can hold only one instance of the activity at a
time — only one such task.
But: if you look at this part of doc at Figure 4, you see that when Activity 2 starts Activity Y (puts that task to foreground), Activity Y was already on the top of the task and will be on the top of the current task, not the root.
I tried this scenario in this simulation app and when I create singleTask activity, it always creates a new task. However, if the only instance already exists, it finishes all activities above this one so the only instance can be the root (and also the only activity in the task).
How could the Activity Y become on the top of the task above the Activity X?
Is there any other reason I'm missing?
PS: I also don't really understand the difference between Task and back stack.
As usual (sigh), the documentation is wrong. In the diagram you referenced, obviously Activity Y can't be defined as singleTask and yet be the top activity in a background task containing 2 activities.
When testing scenarios with special launch modes singleTask and singleInstance, please be aware that taskAffinity plays an important role in this behaviour, as taskAffinity takes priority over special launch modes.
Regarding the difference between "task" and "back stack":
A "task" is a stack of activities that can be manipulated as a whole group.
When you launch an application (assuming that it isn't currently running), Android creates a new task which is in the foreground and contains the root activity of the application you launched.
When that activity starts new activities, these new activities are added to current task (usually, although there are exceptions to this behaviour).
When you press the HOME button, the current task is moved from the foreground to the background.
When you show the list of "recents", what is displayed is the list of recent tasks, not the list of recent activities or list of recent applications.
When you select a task from the list of recent tasks, if that task is still active (still has live activities in it), the entire task (including all of its activities) will be brought from the background to the foreground.
Tasks can also be "stacked". When an activity in the current task starts an activity in a new task, the new task is stacked on top of the current task. This only serves to control what happens when the new task completes. In the usual case, when the new task completes (all of its activities have finished), Android will return the user to the previous task (ie: the task that started the completing task).
A "back stack" usually refers to a set of activities within a task. Each task has its own stack of activities. This is used to control what happens when the current activity (the one on top of the back stack) finishes. Normally Android returns the user to the activity that is directly underneath (below) the finishing activity in the back stack.
The Android code and documentation often refer to the "root" of a task (this is the activity that was used to start the task) and the "top" or "front" of a task (this is the activity that is currently being shown).
Actually, the documentation lies :-( Here's an example:
In contrast, "singleTask" and "singleInstance" activities can only
begin a task.
This statement is usually, but not always correct. For example, let's say I have 2 activities: A and B. A is the launch activity (ie: the one with ACTION=MAIN and CATEGORY=DEFAULT) and is defined with standard launch mode. B is defined with launchMode="singleTask". I start the application and Android creates an instance of A. In A I then do:
startActivity(new Intent(this, B.class));
This will create a new instance of activity B and put it on top of A in the same task. It will not create a new task with activity B as the root. The reason is that activity A and activity B have the same taskAffinity (by default all activities of an application have the same taskAffinity), and Android will ignore the launch mode of B in this case.
The documentation also says:
Moreover, the device can hold only one instance of the activity at a
time — only one such task.
Again, taskAffinity can break this behaviour. Assume again we have A, B and C, all with the same (default) taskAffinity. A and C have standard launch mode, B has launchMode="singleTask". If A starts B, the instance of B ends up not in a new task, but in the same task as A (see above). Now B starts C. Android creates an instance of C and puts it on top of B in the same task. Now C calls:
startActivity(new Intent(this, B.class));
Android creates a new instance of B and puts this on top of C in the task. There are now 2 instances of B and neither of them is the root activity of the task! This behaviour is also due to the fact that taskAffinity trumps launch mode.
Related
I'm trying to understand the Android Tasks and Back stack by reading the official documentation: https://developer.android.com/guide/components/activities/tasks-and-back-stack
I have a couple of questions regarding the documentation.
Question 1: In Figure 4. in the documentation, there are 2 tasks - a foreground and a background task, the background tasks contains Activity Y and X, if the Activity Y is declared with a singleTask launch mode, how is it possible to create a task with activity Y on top of activity X?
For other questions, I prepared a simple project - 2 activities, A and B. On both activities I have 2 buttons:
Button A opens Activity A
Button B opens Activity B
Activity A is the MAIN (LAUNCHER) activity.
Question 2: The documentation says that using the intent flag FLAG_ACTIVITY_NEW_TASK produces the same behavior as using the singleTask launcher mode:
This produces the same behavior as the "singleTask" launchMode value, discussed in the previous section.
This is not what I see from my test application. If the activity B has a launcher mode set to singleTask and if my backstack is A -> B -> A -> A, then if I open B the back stack will look like A -> B (it will pop the last 2 As). I guess that the reason behind it is the tasks created: when the launcher intent was sent it opened the activity A in the new task (let's call it to task 1), when I opened B, it opened it in task 2 (because it has a singleTask launch mode), then the 2 A activities where opened in Task 2 as well. After opening B again, Android found a task that already has a B activity (Task 2) and it brought it to the front, poping 2 As.
However, using a FLAG_ACTIVITY_NEW_TASK instead of singleTask does not produce the same behavior, it just opens B on top of everything else. Is the documentation wrong, or am I doing something wrong?
Question 3: The example at the end says:
the two launch modes that mark activities as always initiating a task, "singleTask" and "singleInstance", should be used only when the activity has an ACTION_MAIN and a CATEGORY_LAUNCHER filter. Imagine, for example, what could happen if the filter is missing: An intent launches a "singleTask" activity, initiating a new task, and the user spends some time working in that task. The user then presses the Home button. The task is now sent to the background and is not visible. Now the user has no way to return to the task, because it is not represented in the app launcher.
How to reproduce that? In my previous example, I open the activity B in a new task, but if I hit the home button and the launcher icon again, I get back to activity B. So even though I'm using a singleTask launcher mode, I'm still able to return to that task by using the launcher icon or selecting it from the list of the recent applications.
It's entirely possible that I'm doing something wrong here and that new tasks are not generated, is there a way to see all tasks and activities for a specific application?
Thanks.
Today I spent more time trying to understand what's going on here and I think I have the answers. I realized that nothing in my example created new tasks. Tasks are visible in the Recent screens (apps in the background) - at least on Android 5+.
So, the first question that should be answered is: Why creating activity B is not creating a new task?
It's because generating new tasks goes hand in hand with the taskAffinity property. If you don't specify this property, your activity will have the default taskAffinity which is your application's package name. When you open the activity with a singleTask launcher mode, Android will look for tasks with the same affinity (task's affinity is defined by its root activity affinity) and if it finds one it will add your activity to that task. Because I didn't specify the affinity, android assumed I want to add my activity to the task with an affinity equal to the application's package and it didn't create new tasks at all. It just added an activity to an existing task.
That being said, FLAG_ACTIVITY_NEW_TASK and singleTask are very much different, although sometimes they produce the same behavior. If you trigger an intent with the FLAG_ACTIVITY_NEW_TASK flag that is trying to open an activity A without specifying activity's taskAffinity you'll just normally add an activity on top of the current task. (This is still confusing to me, I would expect it to open an activity only if the activity is not in the stack; if it is, it should do nothing.)
Opening the same activity without this flag but using the singleTask mode, will again not create a new task but will:
Add the activity on top if it's not in the stack (this is the answer to question 1)
Destroy all activities on top of yours and call onNewIntent if the activity is already in the stack. (I think that branch.io uses this method to handle deep links - which seems really hacky to me, but...)
On the other hand, if you specify the taskAffinity for your activity then:
if singleTask mode is set, android will search for the task with this affinity and if it's present it will
add your activity on top if the activity is not on task's backstack
destroy all activities on top of your activity and call onNewIntent if the activity is in the back stack.
If the task is not present, it will create a new one and add your activity as root.
if FLAG_ACTIVITY_NEW_TASK flag is set in the intent, android will search for the task with the specified affinity and if it finds one it will just bring that task to the foreground, without destroying the stack or calling onNewIntent. (I tested that if the activity is in the back stack, I guess that if the activity is not in the back stack it would push it to the task's stack).
So, to answer question 2. The flag FLAG_ACTIVITY_NEW_TASK and the launcher mode singleTask are different. I guess that similar behavior can be achieved by using 2 more flags FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP.
I think that the example in the documentation regarding question 3 is relevant for older versions of Android (lower than 5.0). There different tasks were not shown in the Recent Screens - here's the documentation: https://developer.android.com/guide/components/activities/recents.
I'm getting extremely confused with activity launch modes. Here is the scenario.
Activity A - main, launcher with singleTask launch mode
Activity B - singleTask launch mode
Activity C - standard launch mode
Now here is what I'm doing
Activity A > Activity B > Activity C
And I press the home button. And I resume the app from recent apps. What actually happens is that
Activity A
But I have read the documentation and it should be like this after resume
Activity A > Activity B
Or am I missing something?
Well, a lot depends on how you have set taskAffinity for the various activities in the manifest.
Let's assume that you configured it so that activity A and activity B have different taskAffinity (which is the way you SHOULD configure it). When you start your app, Android creates a new task containing A. When you launch B from A, Android should create another new task containing only B. The task containing A is sent to the background. You should now have 2 tasks, one contains only A and one contains only B. When you launch C from B, the second task should now contain B->C. When you press the HOME button, the second task is sent to the background.
Now, when you look in the list of recent tasks, you should actually see 2 tasks for your app. Depending on which one you choose, you will get either the task containing only A, or the task containing B->C.
This is why you should NOT use special launch modes singleTask or singleInstance, because they do a lot of stuff that you don't expect. If you ignore this advice and use these launch modes anyway, you must be aware how taskAffinity impacts the way activities are launched into the various tasks, and you also need to be aware that you may end up with multiple tasks, in which case you need to provide a way for the user to return to the correct task from the list of recent tasks. This also means that you may need to provide different labels for the tasks and/or differnt icons for the tasks, in order to help the user find the correct task to resume.
I have read 4 type of activity's launch mode(standard, singleTop, singleTask, singleInstance), and I could imagine how activity will "stacked" depend on its launch mode.
But what's the implication of starting new activity in new stack vs current stack?
e.g. :
I start with opening Activity A, then I want to open Activity B, then I have a choice to put this on top of A(Current Stack) or I could start a new Task for this Activity B(as root activity of the new task). What's the implication?
I believe you meant "Task" instead of Stack in your sentences.
The first obvious implication is that you'll see a new Task appearing in the Task manager and you'll be able to switch between the new task and the old one. The two will exist in parallel.
The implication is that you see both tasks in recents, like having two instances of the app opened.
I have an Activity A which is declared as singleTop and android.intent.action.MAIN in Android Manifest. I start it from launcher, then launch another activity B through Intent and then press Home button. Now I have a task with activity stack "A, B" waiting in background. If I then again start activity A from launcher I get back already running instance with a stack restored (activity B running in foreground).
This is nice. And I want to achieve the same effect when launching activity A from my own Notification. I've tried different combinations of Intent flags but I've got either a new instance of activity A or the same instance but with cleared stack (no activity B in foreground).
First of all, let's make sure that the system won't kill your Activity B when you are not using that task for a long time.
If the user leaves a task for a long time, the system clears the task of all activities except the root activity. When the user returns to the task again, only the root activity is restored. The system behaves this way, because, after an extended amount of time, users likely have abandoned what they were doing before and are returning to the task to begin something new.
There are some activity attributes that you can use to modify this behavior:
alwaysRetainTaskState: If this attribute is set to "true" in the root activity of a task, the default behavior just described does not happen. The task retains all activities in its stack even after a long period. So what you need to do is to set this to true under your <activity> tag in your manifest file for A.
Second, you won't need singleTop. Check this figure from Android docs. I guess this is what you want. If you start an activity that specifies the singleTask launch mode, then if an instance of that activity exists in a background task, that whole task is brought to the foreground. At this point, the back stack now includes all activities from the task brought forward, at the top of the stack.
So you also need to add singleTask under the tag of A and B, instead of singleTop. Then, you need to launch activity A from Notification without any flags but FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_REORDER_TO_FRONT(optional).
I haven't tried this myself but I have a strong belief that it will work. Try it and let me know if it doesn't.
I have doubt with android backstack , please consider the scenario I have two activities A and B in same app ,
start with A.
from A launch B using FLAG_ACTIVITY_NEW_TASK
from B launch A using FLAG_ACTIVITY_NEW_TASK
press Back button 3 times to return to home screen.
My question is how does the backstack work with this flag , is new task created every time and previous task is pushed to background or activity is created on top of same task.
My doubt is if first one is correct then does back button really remove the activity from top of the stack and if second is correct what is the use of that flag in correct sense.
Thanks in advance.
Regards,
Rohit
First of all, launching B using FLAG_ACTIVITY_NEW_TASK will NOT create a new task unless you have explicitly set android:taskAffinity in the manifest for B. The reason for this is that, by default, all Activities in your application have the same taskAffinity and taskAffinity overrides FLAG_ACTIVITY_NEW_TASK.
If you have set the taskAffinity of A and/or B so that they are different (or both empty), then it works like this:
User launches A from homescreen. You now have one task (let's call it TaskX)
A launches B with FLAG_ACTIVITY_NEW_TASK. You now have two tasks (let's call the new task TaskY). TaskX is pushed to the background and TaskY is in the foreground.
B launches A with FLAG_ACTIVITY_NEW_TASK. You still only have two tasks. B (in TaskY) launched A which created a new instance of A and put it in TaskX because the first instance of A has the same taskAffinity as the second instance of A). This also causes TaskX to come to the foreground and TaskY to go to the background.
Press "back". The second instance of A is removed from the stack and you go back to the first instance of A in TaskX. Pressing the "back" key goes back within the current task.
Press "back". The first instance of A is removed from the stack. TaskX is now empty as it has no activities in it. The previous task (TaskY) is brought to the foreground and B is resumed.
Press "back". The instance of B is removed from the stack. TaskY is now empty and the home screen is shown.