Mobile / September 13 , 2022 / Amit Gupta
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.
There are four launch modes for activity.
Figure 1. A representation of how each new activity in a task adds an item to the back stack. When the user presses or gestures Back, the current activity is destroyed and the previous activity resumes.
This is the default launch mode of activity. If you don’t set any launch mode to your activity, it will use the standard mode by default. It creates a new instance of activity every time even if the activity instance is already present.
Suppose we have A, B, C, and D activities and your activity B has Standard launch mode. Now 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
We can see that new instance of B is created again.
If an instance of activity already exists at the top of the current task, a new instance will not be created and the Android system will route the intent information through onNewIntent().
If an instance is not present on top of the task then a new instance will be created.
Suppose we have A, B, C, and D activities. A →B →C →D
If we launch C then a new instance of C will be created as it is not on top.
So it will look like A →B →C →D →C
Now suppose we have A →B →C →D →C like this
then we again launch C activity then in this case a new instance will not be created. Instead, we will receive the callback on onNewIntent() method.
An activity declared with launch mode as singleTask can have only one instance in the system (singleton). At a time only one instance of activity will exist.
If activity instance is not present then the new instance will be created and if the instance is already present in the system then the onNewIntent() method will receive the callback.
Suppose we have A, B, C activities (A →B →C ) and we are launching D that has a singleTask launch mode. In that case, the new instance of D will be created so the current state will look like this. (A →B →C →D)
Now let suppose if we launch B that also have has a singleTask launch mode then current state will
look like this.
Here old instance gets called and intent data route through onNewIntent() callback. Also, notice that C and D activities get destroyed here.
Figure 2. A representation of how an activity with launch mode "singleTask" is added to the back stack. If the activity is already a part of a background task with its own back stack, then the entire back stack also comes forward, on top of the current task.
It is similar to singleTask except that no other activities will be created in the same task. If another Activity is called from this kind of Activity, a new Task would be automatically created to place that new Activity.
Suppose you have A, B, and C activities (A →B →C) and your activity D has a singleInstance launch mode. In this case, if we launch D then D will be launch in the diffrent task. New task for D will be created.
Task1: A →B →C
Task2 : D (here D will be in the different task)
Now if you continue this and start E and D then Stack will look like
Task1: A →B →C →E
Suppose you have A, B, C activities in one task (A →B →C) and activity D is in another task with launch mode singleInstance. In this case, if we launch D again then we will receive the callback in the onNewIntent() method of D activity.
Task1: A →B →C
Task2: D (Here old instance gets called and intent data route through onNewIntent() callback)
Contact Us for your project estimation