Android Task Implication - android

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.

Related

Remove the Background Activity Task in Android which only Task Create But Activity Never Focus

In My App, When i am start creating New Activity-1 as singleInstance. So, it follow below Steps in Android Execution.
(So, Stack look like TASK-A(10000099)-> Activity-1)
I am_create_task: [10,10000099]
I am_create_activity: [10,225706045,10000099,...]
So, before Task A: 1000099 complete the other process to create activity
I am Launching Another New Activity-2 as SingleInstance.
(Stack look like TASK-B -> Activity-2 on Foreground while TASK-A with it's activity in Background). Android Execution to create TASK-B and it's Activity-2 as follow.
am_create_task: [10,1000100]
am_create_activity
am_create_called
am_start_called
am_resume_gained
TASK-B on foreground and than i finish Activity-2 which placed in TASK-B. Hence, TASK-B finished.
am_finish_activity
I am_destroy_activity
I am_remove_task: [1000100,125]
Here is Issue: Now, the TASK-A which left between the process android brings into foreground with below log.
I am_focused_stack: [10,0,124,127,finishActivity adjustFocus adjustFocusToNextFocusableStack]
After this, all the activity process executed and TASK-A -> Activity-1 Become Foreground.
It seems TASK creates and execute based on LIFO as Back Stack activities.
Now, how to stop pop-up of TASK-A and it's Activity-1. I want to finish it in Background only. How to Achieve this.
NOTE: noHistory, clearTaskOnLaunch and finishOnTaskLaunch is not working at all.

Does the Back Stack contain activities of one Task or multiple Tasks?

If I start another activity, that activity may be created in another task. Is this activity in the same Back Stack as the one that created it? Or does every task has it's own Back Stack?
that activity may be created in another task
It depends on the launch mode you set to Activity, Default behavior when you start new activity it will be the same task. In case you use launch mode is "singleTask" it will create new in another task or bring the previous instance to BackStack. Finally, when you call startActivity it always the same BackStack.
You can check documents here, and this it visualizes when startActivity in another Task

How to replicates examples in the Android Tasks and Backstack documentation?

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.

Multiple activities with singleTask

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.

android singleTask activity not as the root?

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.

Categories

Resources