Photo by Andy Hermawan on Unsplash

LaunchModes in Android

Gajendra Singh Rathore

--

Learn about what is LaunchMode and it’s types:-

What is Launch mode?

LaunchMode is an AndroidOS command used to set the way of starting an activity and putting it in BackStack.

Types of Launch Mode in Android?

  • Standard
  • Single Top
  • Single Task
  • Single Instance

Standard

Standard is the default launch mode set to any activity that we add in our android app until we specify a launch mode. If the launch mode of an activity is set as “Standard”, then multiple instances of an activity can be created and stacked on top of each other in the BackStack.

Example of Standard launch mode

In the above example, we’ve B already in the BackStack but still a new instance of activity B is created and added to top of the stack.

Single Top

If an instance of the activity is already present on the top of current task, no new instance will be generated for the activity instead, the same old instance of that activity will receive the intent via onNewIntent() method. If the instance is not present at any level in the current task then just like normal case, an instance of that activity will be created and added to the top of the current task and if the instance of the activity is already present in the current stack but is not on the top, in this case also a new instance of the activity will be created and added to top of the current task.

Check out the below image for clarification of all three cases that we learnt.

Cases for SingleTop launch mode in android

Single Task

In single task launch mode, at any given time only a single instance of an activity will be present in the current task. usually we encounter 2 cases, first one is when the activity is not present in the current stack and we launch the activity, then an instance of that activity will be created and added to the current task top, if the activity already present let’s say ( A →B →C →D) and we launch C from activity D, then activity D will be destroyed and the old instance of C will receive the intent via onNewIntent() method.

Here’s a way to add singleTask launch mode to an activity in AndroidManifest.

Here’s the graphical representation to understand single task completely with examples:-

Cases for SingleTask launch mode in android

Single Instance

Single Instance launch mode is similar to singleTask except that no new instance of the activities will be created in the same task. If an activity is called with singleInstance launch mode then a new task would be automatically created and the new activity will be added to the new task instead of the same task.

Below is the line of code that you need to add to Android Manifest for the activity you want to set launch mode as singleInstance.

Here’s the graphical representation to understand single instance completely with examples:-

Case I- SingleInstance launch mode
Case II — Single Instance launch mode

❤️Finally

I hope learning about launch modes available in Android was helpful, most probably it’ll be clear for you now but still if you have any queries or any use case in mind that you’re not sure about, feel free to write your queries in comments and I’ll be more than happy to help you resolve them. Let me know in the comments if you like the blog and learnt something out of it.
Subscribe for the upcoming ones, many more comming soon.

Thanks :) Be safe, Keep coding Cheers!

--

--