smichak
smichak

Reputation: 4958

Understanding android: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.

Upvotes: 6

Views: 4558

Answers (3)

Shubham Kumar Gupta
Shubham Kumar Gupta

Reputation: 1147

Here's how I remembered this

  • task = Stack that contains Activities
  • task affinity = Task in which the activity pushed into

#launch modes


  • Standard: If A activity is created it will go at top [deafult]

  • singleTop : If A activity is called then if its already in top then no new created, onNewIntent()

  • singleTask -> will be single instance irrespective of task it a instance is there then jump and restore already instance with onRestart and destroy others in line. If Activity B (singleTask),C,D in Task2 and (I’m here) Activity A is in Task1 and If I open Activity B then it will open Task2 and destroy all from top(C,D) until gets ActivityB

  • single Instance when called with this activity will create new task separately for this and no other activity will be put inside this task onNewIntent() ; without task affinity you will see two task but unable to switch between them on tapping recent; with task affinity you will see two task and able to switch between them on tapping recents

  • single Instance Per Task when called with this activity will create new task separately for this and allow other activity to be inside this task

Upvotes: 0

madan yadav
madan yadav

Reputation: 19

Android 4 types of launch mode:- 1.Standard
2.Single Top 3.Single Task 4.Single Instance

  1. Standard -> When go to Activity A to activity B then new object create every time and when go back press go back multiple times.

  2. Single Top -> when user to Activity A to activity B and again go Activity B then new instance not create and onCreate Method not call. but onNewIntent method
    call.

  3. Single Task -> when user to Activity A to activity B and acitivity B to activity C again go Activity B then new instance not create and onCreate Method not call. but onNewIntent method call. and when backpress on activity B then go to activity A. Activity C is remove from mid.

  4. Single Instance -> Activity Stack B is A to B to C. After launching the B activity, A -> B -> C -> D — Job #1 B — Job #2 (Here, B will be assigned to a separate duty)

If you continue like this and add E and B, the stack will look like this: Job #1— A -> C -> D -> E. 5. List item

Upvotes: 0

Keshav Gera
Keshav Gera

Reputation: 11264

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

Upvotes: 2

Related Questions