What is Android Activity “launchMode”?
Launch mode is an Android OS command that determines how the activity should be started. It specifies how every new action should be linked to the existing task. Before proceeding, you must first grasp the following critical topics:
Types of Launch Modes for Activities
Without further ado, coming to the topic, we will be seeing that primarily there are four different types of Launch Modes, which depend upon the type of launch you desire for your Android App. They are viz:
This is the default launch mode of activity (If not specified). It launches a new instance of an activity in the task from which it was launched. Numerous instances of the activity can be generated, and multiple instances of the activity can be assigned to the same or separate tasks. In other words, you can create the same activity multiple times in the same task as well as in different tasks.
<activity android:launchMode=”standard” />
Example: Assume you have A, B, C, and D activities, and Activity B has “launch mode = standard.” You are now launching Activity B once more – State of Activity Stack before Launch B. A to B to C to D. The state of the Activity Stack following startup B.
2. Single Top
If an instance of the activity already exists at the top of the current task in this launch mode, no new instance will be generated, and the Android system will send the intent data through onNewIntent (). If an instance does not exist on top of the task, a new instance will be generated. You can use this launch mode to generate numerous instances of the same activity within the same task or across tasks, but only if the identical instance does not already exist at the top of the stack.
<activity android:launchMode=”singleTop” />
Example: Assume you have A, B, C, and D activities, with D having “launch mode = singleTop.” You are now resuming your activity. D – The state of the Activity Stack before starting D is A to B to C to D. After launching the D activity, the state of the Activity Stack is as follows: A to B to C to D (Here, the old instance is invoked, and the intent data is sent through the onNewIntent() callback.)
3. Single Task
In this method of operation, a new task is always generated, and a new instance is added to the task as the root one. If the activity already exists on another task, no new instance is created, and the Android system transmits the intent information via the onNewIntent() function. At any one time, there will be just one instance of the activity.
<activity android:launchMode=”singleTask” />
Example: Assume you have activities A, B, and C, and your activity D has “launch mode = just one job ” You are about to begin an activity. D – The state of the Activity Stack before start D is A to B to C. After launching the D activity, the state of the Activity Stack is as follows: A to B to C to D (As usual, D launches here.)
Example2: Now Let suppose if we launch B that also has singleTask launch mode then current state will look like this
Here old instance gets called and intent data through onNewIntent() callback. Also notice that C and D activities get destroyed here
4. Single Instance
This is a highly unique start option that is only used in programs with a single activity. It works similarly to Single Task, except that no additional activities are generated in the same task. Any further activity initiated from this point will result in the creation of a new task.
<activity android:launchMode=”singleInstance” />
Example: Before start, the state of the Activity Stack D is A to B to C. After launching the D activity, the state of the Activity Stack is as follows:
A -> B -> C — Job #1
D — Job #2 (Here, D will be assigned to a separate duty)
If you continue like this and add E and D, the stack will look like this: Job #1— A -> B -> C -> E.
To manage the launch tags you can use these methods:
By using the above methods you can programmatically launch tasks, without mentioning the launch type in the Android Manifest!