Understanding the launch mode of Activity in Android

Sharing is caring!

Launch mode of activity is playing an important role to maintain the task and back stack. Before to explain the launch mode we need to first understand what is the task and back stack?

Task

A task is a “stack of Activities” in your application. If an Activity is sent to the background (by pressing the HOME key whilst viewing it, for example) then the whole Task (and all the Activities inside it) will be sent back as well. If the user then clicks on your application, the task (and the order of its activities) come forward. In other words,  A task is a collection of activities that users interact with when performing a certain job. The activities are arranged in a stack—the back stack)—in the order in which each activity is opened.

So basically When a user launches an app from home icon, it navigates through different screens so different activities placed on the top of one another. This collection of activities is known as tasks.

Back Stack

Activities are arranged in the order in which each activity is opened. This maintained stack called Back Stack. When you start a new activity using startActivity(), it “pushes” a new activity onto your task, and put the previous Activity in the back stack.

Once you press back button then “pops” the topmost activity and remove it from the back stack and taking you back to the previous activity.

Launch Mode

A “launch mode” is the way in which a new instance of an activity is to be associated with the current task. Launch modes may be defined using one of two mechanisms:

  1. By declaring in AndroidManifest.xml file
  2. By adding the flag with Intent.

There are four types if launch mode of activity. These are:

  1. standard
  2. singleTop
  3. singleTask
  4. SingleInstance

Manifest file: When declaring an activity in a manifest file, you can specify how the activity should associate with tasks when it starts.

Exp:

<activity android:launchMode = [“standard” | “singleTop” | “singleTask” | “singleInstance”] ../>

Intent Flag: Android also provides Activity flags by which you can change the default behavior of Activity association with Tasks while starting it via startActivity() method. These flags values can be pass through Intent extra data.

FLAG_ACTIVITY_NEW_TASK ->  similar to “launchMode = singleTask”
FLAG_ACTIVITY_CLEAR_TASK
FLAG_ACTIVITY_SINGLE_TOP -> similar to “launchMode = singleTop”
FLAG_ACTIVITY_CLEAR_TOP

standard (default) : This is the common mode for most of the activities.Multiple instances of the activity class can be instantiated and multiple instances can be added to the same task or different tasks.

Example:
Suppose you have A, B, C, and D activities and your activity B has “launch mode = standard”. Now you again launching activity B –
State of Activity Stack before launch B
A -> B -> C -> D
State of Activity Stack after launch B
A -> B -> C -> D -> B

singleTop : The difference from the standard is if an instance of the activity already exists at the top of the current task and the system routes the intent to this activity, no new instance will be created because it will fire off a methodonNewIntent() instead of creating a new object.

Example:
Case 1:
Suppose you have A, B, and C activities and your activity D has “launch mode = singleTop”. Now you launching activity D –
State of Activity Stack before launch D
A -> B -> C
State of Activity Stack after launch D activity
A -> B -> C -> D (Here D launch as usual)
Case 2:
Suppose you have A, B, C, and D activities and your activity D has “launch mode = singleTop”. Now you again launching activity D –
State of Activity Stack before launch D
A -> B -> C -> D
State of Activity Stack after launch D activity
A -> B -> C -> D (Here old instance gets called and intent data route through onNewIntent() callback)

singleTask : A new task will always be created and a new instance will be pushed to the task as the root. However, if any activity instance exists in any tasks, the system routes the intent to that activity instance through the methodonNewIntent() call. In this mode, activity instances can be pushed to the same task. This mode is useful for activities that act as the entry points.

Example:
Case 1:
Suppose you have A, B and C activities and your activity D has “launch mode = singleTask”. Now you launching activity D –
State of Activity Stack before launch D
A -> B -> C
State of Activity Stack after launch D activity
A -> B -> C -> D (Here D launch as usual)
Case 2:
Suppose you have A, B, C, and D activities and your activity B has “launch mode = singleTask”. Now you again launching activity B-
State of Activity Stack before launch D
A -> B -> C -> D
State of Activity Stack after launch B activity
A -> B (Here old instance gets called and intent data route through onNewIntent() callback)
Also, notice that C and D activities get destroyed here.

singleInstance : Same as,singleTask except that the no activities instance can be pushed into the same task of the singleInstance’s. Accordingly, the activity with launch mode is always in a single activity instance task. This is a very specialized mode and should only be used in applications that are implemented entirely as one activity.

Example:
Case 1:
Suppose you have A, B and C activities and your activity D has “launch mode = singleInstance”. Now you launching activity D –
State of Activity Stack before launch D
A -> B -> C
State of Activity Stack after launch D activity
Task1—A -> B -> C
Task2—D (here D will be in different task)
Now if you continue this and start E and D then Stack will look like-
Task1—A -> B -> C -> E
Task2—D
Case 2:
Suppose you have A, B, C activities in one task and activity D is in another task with “launch mode = singleInstance”. Now you again launching activity D-
State of Activity Stack before launch D
Task1—A -> B -> C
Task2—D
State of Activity Stack after launch B activity
Task1—A -> B -> C
Task2—D (Here old instance gets called and intent data route through onNewIntent() callback)

FLAG_ACTIVITY_NEW_TASK : Same as the valuesingleTask in the Manifest file (see above).

FLAG_ACTIVITY_SINGLE_TOP : Same as the valuesingleTop in the Manifest file (see above).

FLAG_ACTIVITY_CLEAR_TOP : If the activity being started is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it are destroyed and this intent is delivered to the resumed instance of the activity (now on top), though.onNewIntent() There is no corresponding value in the Manifest file that produces this behavior. I used reference for this article are here and here.

Please do subscribe email to get all newsletters of this blog and if you feel that this post will help you to better understand then do not forget to subscribe, share and comment below. Ok, then I will see you in my next tutorial till then enjoy your life and happy coding 🙂

0 0 votes
Article Rating

Similar Posts

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments