In this article I will be discussing Android Activity launch modes, why we need launch mode, what are the different types of launch modes are available, and how they work. Before we dive into that I would like to introduce some topic we often use in this tutorial. Task and Back Stack. In this context Task means collections of activities in an app, and Back Stack means as the name suggest is a stack which does only two operation push/pop in “last in, last out” fashion. An example of Back Stack as follows :
In an android application, it may have multiple task within the applications, and each task has a back stack which holds the activities in LIFO manner. The activities are pushed based on order the user opened them. For example, lets say we have three activities in an application, lets call it Activity A, B, and C. When the app launches the Activity A will open, then it will be pushed into the back stack, from Activity A the user opens the Activity B, then it will be pushed into the back stack, and finally from the Activity B the user opens the Activity C, then it will be pushed into the back stack. Now the back stack will look like the image above. If the user decided to go back to the Activity B from the Activity C, by pressing the back button. The Activity C will be popped from the stack by calling it finish (Destroyed). Now the stack will look like this :
The Activity B will be visible to the user. I hope you got the mechanism of the back stack.
Let's talk about why we need a launch mode for the activity. By default, each time you open an activity, android system will create new instance of that activity. For example, if the Activity A has already opened, and the user is currently on that screen. Let's say if we need to open Activity A again, the android system does not use the existing Activity A, it will create another new instance of the Activity A. Illustration Below might help you to understand:
As you can see we have the same activity that's being pushed twice (New Instance) into the back stack. We can customize this natural workflow, so we can use an existing activity instead of creating a new one. So far all the activities existed in the same task. Like I mentioned earlier, that an app can have a multiple task, if we want an activity to start its own task and have back stack we can achieve this by utilizing the features of launch modes, will discuss this later. For example, from the Activity A the user launches the Activity B, which will start its own task and back stack. An illustration below :
Despite we have two different task /back stack. If the user presses back button, it will always go back to the previous activity as usual in this case from the Activity B to Activity A. I hope you have understood or have an idea of why we need launch modes.
So let’s talk about types of launch modes, and how to define them in an app. Android provides four types of launch modes. I will cover them individually. The four types of launch modes are follows :
- standard (By default activities use this)
There are two ways to define the launch modes. First way is using Android Manifest file, and second way is using Intent Flags. I will be primarily discussing how to define in the Manifest file. If you want to know how to define them using an intent, please refer here.
Inside the Manifest file, you can add the launch mode inside the attribute of activity tag. Image below illustrates this :
Let’s discuss each launch modes in detail.
This the default launch mode of the activity. This creates a new instance of an activity even if the activity already exists in the back stack. For example, if the Activity A already exists in the back stack, and if the user wants to open the same Activity A again, android system will create new instance of the Activity A although its already exists. After creating the second instance of the Activity A, again if the user opens the Activity A, the android system will create another new instance of the Activity A although there is already two activity exists in the back stack. At the end the system has created three activity of Activity A. Image below illustrates the back stack :
This launch mode is pretty much similar to the previous one but it differs in a scenario when the activity that we want to launch that already exists and it’s on the top of the back stack. Instead of creating a new instance of the requested activity, android system will use the existing activity that's on the top of the back stack. If the activity does/do not exist and it’s not on the top of the back stack, Android system will create new instance of that activity. Let see the example of these scenarios.
The Activity exists and it’s sitting on the top of the back stack (Assume that's current activity as well). For example, the Activity C exists, and it’s on the top of the back stack. The user wants to go to the same activity again. The Android system will not create new instance of the Activity C, instead it will use the existing Activity C that’s sitting on the top of the back stack. Hence, the back stack remains intact. Image below illustrates :
The Activity exists, and but it’s not on the top of the back stack. For example, Activity C exists, and it’s not on the top of the stack. The user wants to go to the Activity C from Activity B. Android system will create a new instance of the Activity C, even though Activity C already exists in the back stack, but it's not on the top of the back stack. Hence, the back stack is modified by adding new instance of the Activity C. Image below illustrates:
If the Activity does not exist on the back stack, and Android system will create new instance of that activity.
If the Activity has a launch mode as “singleTask”, when the activity is launched it will initiate a its own task and back stack. If the Activity exists in a different task, the system will route to the existing task by utilizing its onNewIntent() method, hence new task/back stack will not be created. Only one instance of this activity will be created.
The activity does not exist, and hence the new activity with its own task and back stack will be initiated. The Activity will be the root of the new task. For example, we want to open Activity C from Activity B, but it does not exist in the back stack or separate task. So new task /back stack will be created with root activity as Activity C. Image illustrates below :
From this new back stack the Activity C can launch other activities. For example, from Activity C if the user wants to see Activity D which has a default launch mode, it can launch itself. Image below illustrates :
If the user presses the back button, Activity D will be popped off from the back stack of new task, hence Activity C will be visible. So regardless of task, back button will always take the user to previous activity.
The user wants to launch an activity which has a “singleTask” mode, and already exists with its task and back stack. For example, referring to the previous scenario where the Activity C exists with newly created task and back stack. Let’s say the user wants to go to the Activity C from Activity A, the android system will not create a new instance of the Activity C along with the task /back stack. Instead, it just routes it to the Activity C, and what ever activities that was sitting on the top of the root activity will be popped off. Image below illustrates this:
The Activity exists, and it’s not on a separate task/back stack, but its on the current back stack. Let’s say we have 4 Activities in our stack respectively Activity A, Activity B, Activity C, and Activity D. Let’s say that the user wants to go to Activity A from Activity D, since the Activity A has mode of singleTask, and it is exists in the root of the back stack. So no need to create new task/back stack, what would happen in this case is that the system will pop off every activity that’s sitting top of Activity A until it becomes top of the back stack. Hence, the Activity A is being reused instead of creating new one.
This mode is pretty much the same as the previous one, but newly created task cannot launch other activities. So only that instance of the activity exists in the task/back stack. For example, like the previous example we have launched the Activity C from the Activity B but in this case the Activity C has a mode of “singleInstance”. After creating new a task and back stack which has Activity C, lets say we want to launch the Activity D which has a default mode, the Activity D will be launched pushed into the old task or separate task not the newly created task by the Activity C. This is illustrated below :
I hope this tutorial was helpful. Thank you for reading. If I made any mistake please let me know.