Android Activity Launch Mode Example: Detailed Explaination

We often get confused with Android Activity Launch Mode when we see android:launchMode attribute associated with <activity> element in manifest file, it defines the way it will be associated to a task. A task is a collection of activities that users interact with when performing a certain job. Task uses Stack to maintain the history of activities.

Android Activity Launch Mode can be opted from any four modes as described below.

Standard (Default Launch Mode If not specified)

<activity android:launchMode="standard" />

A new instance of actvity is created everytime in the task from which it was started. Multiple instances of activity can be created and each instance may belong to different task.

SingleTop

<activity android:launchMode="singleTop" />

If instance of activity is present on top of Task stack, a new instance will not be created and system will route your intent information through onNewIntent(). If it is not presnt on top, a new instance will be created. Multiple instance can be created and each instance may belong to different task.

SingleTask

<activity android:launchMode="singleTask" />

A new task will be created and a new instance of the activity will be pushed at root of new task. If instance exists on the seperate task then system routes the call to existing instance through onNewIntent() method. Only one instance will exist at a time.

SingleInstance

<activity android:launchMode="singleInstance" />

Only one instance will exist at a time. System will not launch any other activity into task holding this type. It is always a single member of its task and activities started from here will open into seperate task.

Sample Android Application to Understand Activity Launch Mode

 

 

ActivityLaunchMode_SamleApp

 

1. Create a  new android application project and define four activities in your application manifest one for each launch mode. 

2. In layout file, add four Button views for starting activity of different launch mode type. Final layout file /res/layout/activity_main.xml will be as below.

3. Create four activity classes as per name attribute declared in manifest file. Activity classes will implement View.OnClickListener interface to listen callback for button onClick events. All will have use same layout file and will exhibit same functionality to launch all activities of all four launch modes. Source code for activity class will be as below.

Let’s try understand android activity launch mode in detail with above sample application. For rest of the explanation, I will use following notation LaunchModeActivity(ActivityRecord#). ActivityRecord represents an entry in the history stack of a task, representing an activity.

Example Notations:
(StandardModeActivity.java) “standard” – SM(4b1a9d40)
(SingleTopActivity.java) “singleTop” – STop(4b1e36fc)
(SingleTaskActivity.java) “singleTask” – STask(4b204d60)
(SingleInstanceActivity.java) “singleInstance” – SI(4b26f8b0)

1. Start standard Mode Activity, it will be associated with a new task if no task record exist. In my case, Task # 15 is created and an instance of new activity is pushed onto stack.

Activity_LaunchMode_Step1

2. Start singleTop mode activity from standard mode activity, a new instance for singleTop will be created and pushed to stack.

Activity_LaunchMode_Step2

3. Start singleTask mode activity from singleTop activity, a new instance will be created for it and will be pushed onto stack of current task.

Activity_LaunchMode_Step3

4. Start singleInstance mode activity from singleTask activity. Recall that for singleInstance mode, a new task will be created and a new instance will be created. A new Task# 16 will be created and an instance of singleInstance activity will be pushed onto its stack.

Activity_LaunchMode_Step4

5. Start standard mode activity from singleInstance activity, a new instance will be created and ActivityRecord will be pushed onto stack. Recall for activity started from singleInstance, activity record will be pushed to another task.

Activity_LaunchMode_Step5

It can be observed that Task# 15 now have two instance of standard mode activity with different ActivityRecord number.

6. Launch a new standard mode activity from existing standard mode activity, again a new instance of it will be created and pushed onto stack. Now total number of instance for standard mode will be three. As stated earlier, standard mode can have multiple instances.

Activity_LaunchMode_Step6

7. Now start singleTop activity from current standardMode activity. Since there is no instance on top of the stack, a new instance will be created and pushed onto stack. Now number of instances existing for singleTop mode activity will  be two i.e. it can also have multiple instances.

Activity_LaunchMode_Step7

8. Lets try to realize difference between standard mode and singleTop mode. Start singleTop mode activity from current singleTop activity, no new instance for it will be created rather system will forward call via onNewIntent() method. ActivityRecord stack will remain same after this. You can observe that activity record number also remains unchanged.

Activity_LaunchMode_Step7

9. Now lets try to understand singleTask launch mode behavior as stated earlier. Start singleTask activity from current singleTop activity. Recall that only one instance of activity will exist for singleTask mode and it will be pushed as root of  task.

Activity_LaunchMode_Step9

You can observe that it has cleared activity record on top of it and is now root of the task. ActivityRecord number remains unchanged since an instance was already existing in task.

10. Let’s revisit singleInstance mode, start singleInstance mode activity from current singleTask activity. We already have an instance existing in Task#16 , system will route call via onNewIntent() method to this activity. Same instance will be brought to foreground and will be visible to user. It can be observed that ActivityRecord number is unchanged.

Activity_LaunchMode_Step10

We did it. Now it should be pretty clear about concept of different activity launch mode and its association with a task.

For any queries, Please feel free to comment below.

You may also like...

  • Pingback: Android activity is running twice - CSS PHP()

  • vivek

    Great explanation, Thanks a ton!

  • Sajid Khan

    Very nice post. Great.

  • Sameer Yadav

    Awesome post. very detailed and clear explanation.

  • Dilip Chandar

    Very good explanation. Able to understand clearly.

  • KC Raju Vysyaraju

    Awesome explanation. (Y)

  • Satish J

    crystal clear explanation, thanks for the article

  • Sequel One Solutions Pvt Ltd

    Like that awesome explanation thanks.