Skip to content

Instantly share code, notes, and snippets.

@girish3
Last active June 17, 2019 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save girish3/5943e816b4818a04b1c1a1e9264cf25a to your computer and use it in GitHub Desktop.
Save girish3/5943e816b4818a04b1c1a1e9264cf25a to your computer and use it in GitHub Desktop.
[Activity Launch Mode] Understanding Activity's launch mode #android #android_tutorial #tutorial

Since each Activity is made to work in different purpose. Some is designed to work separately with each Intent sent for example an Activity for email composing in email client. While some is designed to work as a singleton for example an email's inbox Activity. That's why it does matter to specify whether Activity is needed to be created a new one or to use the existed one. launchMode is designed for this specifically.

launchMode can be assigned directly as an attribute tag

<activity
    android:name=".SingleTaskActivity"
    android:label="singleTask launchMode"
    android:launchMode="singleTask">

There are 4 types of launch modes:

standard

This is a default launch mode. Everytime a new activity instance will be created in the same task. A task is just a stack of activities.

singleTop (single activity instance at the top)

It acts almost the same as standard one which means that singleTop Activity instance could be created as many as we want. Only difference is if there already is an Activity instance with the same type at the top of stack in the caller Task, there would not be any new Activity created, instead an Intent will be sent to an existed Activity instance through onNewIntent() method.

singleTask (single activity instance in the whole task or stack)

An Activity with singleTask launchMode is allowed to have only one instance in the system (a.k.a. Singleton). If there is an existing activity in-between the task stack, all of Activities placed above that singleTask Activity would be automatically and cruelly destroyed in the proper way (lifecycle trigged) to make that an Activity we want to appear on top of stack. In the mean time, an Intent would be sent to the singleTask Activity through the lovely onNewIntent() method. If you want to create the new task and instantiate the activity at the root of the new task. You need to set the taskAffinity to empty string, which means the activity has no affinity for any task, so everytime new task will be created. Read more about taskAffinity here

<activity
    android:name=".SingleTaskActivity"
    android:label="singleTask launchMode"
    android:launchMode="singleTask"
    android:taskAffinity="">

singleInstance

Its similar to singleTask, just that whenever singleInstance activity is instantiated, new task will be created evertime. Likewise, if another activity is called from singleInstance activity. There is some issue as you will see only one task in the task manager, just add taskAffinitity with empty string to enable mutliple tasks on task manager.

Ref:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment