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.
Also See: Fastest Android Launchers
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
1. Create a new android application project and define four activities in your application manifest one for each launch mode.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.androidsrc.activitylaunchmode" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.androidsrc.activitylaunchmode.StandardModeActivity" android:label="@string/app_name" android:launchMode="standard" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.androidsrc.activitylaunchmode.SingleTaskActivity" android:launchMode="singleTask" > </activity> <activity android:name="com.androidsrc.activitylaunchmode.SingleTopActivity" android:launchMode="singleTop" > </activity> <activity android:name="com.androidsrc.activitylaunchmode.SingleInstanceActivity" android:launchMode="singleInstance" > </activity> </application> </manifest> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="20dp" tools:context="com.androidsrc.activitylaunchmode.MainActivity" > <Button android:id="@+id/standardMode" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:onClick="onClick" android:text="Standard Mode" /> <Button android:id="@+id/singleTop" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:onClick="onClick" android:text="Single Top" /> <Button android:id="@+id/singleTask" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:onClick="onClick" android:text="Single Task" /> <Button android:id="@+id/singleInstance" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:onClick="onClick" android:text="Single Instance" /> </LinearLayout> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package com.androidsrc.activitylaunchmode; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; public class StandardModeActivity extends Activity implements OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public void onClick(View v) { int viewId = v.getId(); switch (viewId) { case R.id.standardMode: startActivity(new Intent(this, StandardModeActivity.class)); break; case R.id.singleTop: startActivity(new Intent(this, SingleTopActivity.class)); break; case R.id.singleInstance: startActivity(new Intent(this, SingleInstanceActivity.class)); break; case R.id.singleTask: startActivity(new Intent(this, SingleTaskActivity.class)); break; } } } |
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.
2. Start singleTop mode activity from standard mode activity, a new instance for singleTop will be created and pushed to stack.
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.
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.
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.
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.
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.
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.
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.
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.
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.