What happens when you click on an application's launch icon?
Asked Answered
L

2

18

 What happens when you click on an app's launch icon?

  1. Is a new intent always sent, or is the result sometimes the same as resuming a task from recent tasks?

  2. If an intent is sent, when does it get sent to the onCreate() method of a new activity instance and when does it get routed through onNewIntent() of an existing activity?

  3. Let's suppose the intent gets routed through onNewIntent() of an existing activity in the task. Which activity does it get sent to? The one nearest the top or the one nearest the root? Will it always get sent to an instance of the application's launch activity or can it sometimes get sent to an activity with the same affinity as the root? Can it ever get sent to an activity which does not share the same affinity as the root?

  4. Finally, how is this all affected by the various launch modes (standard, single top, single instance, single task) of the activities in the task?

If there is anyone out there who understands all this, please help me!

Losse answered 31/8, 2014 at 18:19 Comment(1)
I think you can dive into the source code of ActivityManager.Constant
E
25
What happens when you click on an app's launch icon?

Launcher apps calls startActivity with an intent [action = Intent.ACTION_MAIN, category = Intent.CATEGORY_LAUNCHER and flag = Intent.FLAG_ACTIVITY_NEW_TASK].

Regarding Intent.FLAG_ACTIVITY_NEW_TASK, from docs:

When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started; instead, the current task will simply be brought to the front of the screen with the state it was last in.

onNewIntent basics:

onNewIntent is delivered only when activity has set either singleTask, singleInstance launch modes. It is also delivered if activity has set singleTop launch mode or the intent to start the activity has set the flag FLAG_ACTIVITY_SINGLE_TOP and the activity instance is already at the top of the target task. It means an attempt was made to launch a new instance of activity, instead the existing instance itself need to handle the intent.

Here is the response to your queries:

Is a new intent always sent, or is the result sometimes the same as resuming a task from recent tasks?

If the task is already running, it is brought to foreground. In case FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET flag was used to launch a activity and latter the task is brought to foreground, then the activity is killed. From docs:

This is useful for cases where you have a logical break in your application. For example, an e-mail application may have a command to view an attachment, which launches an image view activity to display it. This activity should be part of the e-mail application's task, since it is a part of the task the user is involved in. However, if the user leaves that task, and later selects the e-mail app from home, we may like them to return to the conversation they were viewing, not the picture attachment, since that is confusing. By setting this flag when launching the image viewer, that viewer and any activities it starts will be removed the next time the user returns to mail.

-

If an intent is sent, when does it get sent to the onCreate() method of a new activity instance and when does it get routed through onNewIntent() of an existing activity?

onCreate is called while creating a new instance of activity. onNewIntent is called if already an activity instance exists and no new instance need to be created, as in case of singleInstance, singleTask and conditionally singleTop (as described above).

Let's suppose the intent gets routed through onNewIntent() of an existing activity in the task. Which activity does it get sent to? The one nearest the top or the one nearest the root? Will it always get sent to an instance of the application's launch activity or can it sometimes get sent to an activity with the same affinity as the root? Can it ever get sent to an activity which does not share the same affinity as the root?

In case of singleTask and singleInstance it has to be root of the task. In case of singleTop it has to be top activity of the task.

Finally, how is this all affected by the various launch modes (standard, single top, single instance, single task) of the activities in the task?

I hope the explanation provided till now, answers it.

Update 1:

Here is the Launcher code which adds the flags to intent:

void processShortcut(Intent intent) {
    ....
    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    ....
}

void startActivitySafely(Intent intent) {
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    ...
    startActivity(intent);
}
Effort answered 3/9, 2014 at 2:58 Comment(4)
It's a great answer. By logging the value of Integer.toHexString(intent.getFlags()) in onCreate() and onNewIntent() methods you find that there are actually more flags than you mention. You get the value 0x10200000 in onCreate() and 0x10600000 in onNewIntent(). This means that there are always the 2 flags FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_RESET_TASK_IF_NEEDED, and for singleTask and singleInstance there is also the flag FLAG_ACTIVITY_BROUGHT_TO_FRONT. The presence of FLAG_ACTIVITY_RESET_TASK_IF_NEEDED is excellent as it confirms what you said about FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET.Losse
I wish I could find where in the documentation it says which flags are set. It's quite important to know, as some of them have an absolutely devastating effect!Losse
I can't award the bounty yet, but if I don't discover any other flags in the next 11 hours that mess up this explanation, the 100 points is yours!Losse
@pbabcdefp Document specifies that FLAG_ACTIVITY_BROUGHT_TO_FRONT is set for singleTaskMode. We will have to dig into Intent documentation to understand more. BTW thanks for the question, I had some good learning.Effort
M
0

Your best bet is to read through the Developer docs here: http://developer.android.com/training/basics/activity-lifecycle/index.html

There is a flow chart in the first lesson(http://developer.android.com/images/training/basics/basic-lifecycle.png) which provides an excellent graphical representation of the Android activity life-cycle.

Mucor answered 3/9, 2014 at 3:43 Comment(3)
Thanks, but my question is more about tasks than individual activities. Sadly, the documentation doesn't even begin to explain how any of this works.Losse
Are you referring to a collection of activities when you refer to Tasks? developer.android.com/guide/components/…Mucor
Yes I am. Tasks are like stacks of activities, but they are very poorly explained in the docs. This answer, and in particular the slideshow it links to contain loads of really useful info https://mcmap.net/q/44871/-android-task-affinity-explanationLosse

© 2022 - 2024 — McMap. All rights reserved.