Understanding android:launchMode

2014-02-24 android android-activity activity-stack launchmode

I'm getting really confused about the different activity launchModes in Android. I understand it has something to do with the activity stack which is also something not so clear to me. I would appreciate a short explanation about each launchMode with a simple use case.

Answers

android:launchMode is An instruction theta how the activity should be launched. There are four modes with activity flags (FLAG_ACTIVITY_* constants) in Intent objects to determine what should happen when the activity is called upon to handle an intent. They are as follows:

1) "standard" -the default,The system always creates a new instance of the activity in the target task and routes the intent to it.

2) "singleTop" - means activity will be created once and will be on top.If an instance of the activity already exists at the top of the target task, the system routes the intent to that instance through a call to its onNewIntent() method, rather than creating a new instance of the activity.

3) "singleTask" - exclusive for the single task means no multiple instances. The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one.

4) "singleInstance" - No multiple instances,Same as "singleTask", except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task.

The default mode is "standard".

visit for more

http://www.slideshare.net/JAX_London/android-android-activity-launch-modes-and-tasks-gonalo-silva

http://blog.akquinet.de/2010/02/17/android-activites-and-tasks-series-an-introduction-to-androids-ui-component-model/

http://developer.android.com/guide/topics/manifest/activity-element.html

http://www.intridea.com/blog/2011/6/16/android-understanding-activity-launchmode

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.

More Details

<application
    android:name=".MyApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity android:name=".Standard">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".SingleTop"
        android:launchMode="singleTop" />

    <activity
        android:name=".SingleTask"
        android:launchMode="singleTask"
        android:taskAffinity="" />

    <activity
        android:name=".SingleInstance"
        android:launchMode="singleInstance" />      <!--//TODO launchMode -->

</application>

<uses-permission android:name="android.permission.GET_TASKS" />

Sample Demo

Related