Android Launch Modes

Sertaç Ayhan
Huawei Developers
Published in
3 min readDec 29, 2023

Introduction

Hello friends, today we will try to cover one of the fundamental concepts of Android, launch modes.

In this article, I tried to explain the concepts as simply as possible. Frankly, I think every Android developer should have a solid knowledge of tasks, backstack and launch modes. This thought made me think that I should write this article. I hope it will be instructive both for myself and for friends who are new to Android development.

Let’s start.

Tasks and Backstack

Simply, task is a collection of activities that users interact with when trying to do something in your app.

The device Home screen is the starting place for most tasks. When a user touches the icon for an app or shortcut in the app launcher or on the Home screen, that app’s task comes to the foreground. If no task exists for the app, then a new task is created and the main activity for that app opens as the root activity in the stack.

The backstack is a stack that that stores the activities in the order in which they were opened. When the user presses the “Back” button, the top activity is removed from the stack, and the previous one becomes active.

If the user presses Back repeatedly, each activity in the stack is popped off to show the one before it, until the user returns to the Home screen (or to whichever activity was running when the task began). The task is no longer active after all actions have been removed from the stack.

Note: The first codeblocks demonstrates defining a launch mode in AndroidManifest.xml, second are calling an activity with intent flags to define launch mode.

Launch Modes

  1. Standard
  • Each time a new instance of the activity is created, it’s placed on the top of the back stack.
  • If an instance of the activity already exists in the stack, a new instance is created, and both instances remain in the stack.
<activity android:launchMode=”standard” />
val intent = Intent(this, MyActivity::class.java)
startActivity(intent)
  • Standard launch mode is well-suited for activities that are independent and don’t have specific requirements regarding their instance creation or placement in the task stack.
  • Well-suited for activities that are independent and don’t have a specific navigation requirement.

2. singleTop

  • If an instance of the activity already exists at the top of the stack, no new instance is created.
  • If the activity is not at the top of the stack, a new instance is created and placed on top.

<activity android:launchMode=”singleTop” />
val intent = Intent(this, MyActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
startActivity(intent)
  • Useful in scenarios where you want to ensure that only one instance of a particular activity exists at the top of the activity stack
  • Suitable for activities that handle events or notifications and need to update their UI.

3. singleTask

  • A new task is created, and a new instance of the activity is placed at the root of the new task.
  • If an instance of the activity already exists in a different task, the existing instance is reused, and all other activities on top of it are cleared.

<activity android:launchMode="singleTask" />
val intent = Intent(this, MyActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
  • Ideal for the main entry point of your application, like the home screen.
  • Ensures that only one instance of the activity exists in the entire application, reducing redundancy.
  • Helpful for activities that represent different sections or categories of your app.

4. singleInstance

Similar to SingleTask, but the activity is placed in a separate task. No other activities are allowed in the same task.

<activity android:launchMode="singleInstance" />
val intent = Intent(this, MyActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_INSTANCE)
startActivity(intent)
  • Appropriate for activities that should exist in isolation and not be part of a task with other activities.
  • Suitable for activities that perform specific, standalone tasks, such as a dialer or camera.
  • Use with caution, as it creates a separate task, and only one instance of the activity can exist.

Conclusion

We have covered the concepts as simply as possible. The examples here are given to better explain the concept, usage types may vary depending on application needs.

References

--

--