Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

 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!

like image 581
Paul Boddington Avatar asked Aug 31 '14 18:08

Paul Boddington


People also ask

What happens when an Android app is launched?

An Android process is started whenever it is required. Any time a user or some other system component requests a component (could be a service, an activity or an intent receiver) that belongs to your application be executed, the Android system spins off a new process for your app if it's not already running.

What is Launch icon?

A Launcher icon is a graphic that represents your application on the device's Home screen and in the Launcher window. The user opens the Launcher by touching the icon at the bottom of the Home screen. The Launcher opens and exposes the icons for all of the installed applications.

What code is executed when an Android application is launched when a user selects the application's icon?

So although the call stack of an Android application start at ZygoteInit. main, the code executed in ZygoteInit. main begins after the call to runSelectLoop instead of the beginning of ZygoteInit. main.

What is the app launcher?

An app launcher replaces the stock user interface for organizing the home screen and app icons predominantly in the Android world; however, they are also available for jailbroken iPhones (see iPhone jailbreaking and Cydia).


1 Answers

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);
}
like image 135
Manish Mulimani Avatar answered Sep 20 '22 12:09

Manish Mulimani