Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Launch modes of Android Activity
Mohit Rajput
Mohit Rajput

Posted on

     

Launch modes of Android Activity

Welcome to one of the most important topics of Android application development. In this article we will see detailed explanation of launch modes with examples.

Important terms

Here are a few important terms before jumping on to launch modes.

Task

A task is a collection of activities. An application can have single or multiple tasks. On application launch, a new task is created and launcher activity becomes the root of the task. New task creation depends upon launch modes which we will see later in this article.

Back Stack

Maintaining activities in the stack data structure is called the back stack. Activities can be pushed into the stack and popped from it based on user actions and launch modes.

Launch Modes

There are four types of launch modes:

  1. Standard
  2. Single top
  3. Single task
  4. Single instance

Let's understand each of them.

1. Standard

When you don't specify any launch mode,standard is the default one. It creates a new instance of the activity every time you start it. Suppose A, B, C, D, etc. are activities. It works as follows:

The task with activities:

A -> B -> C -> D
Enter fullscreen modeExit fullscreen mode

Start new activity E:

A -> B -> C -> D -> E
Enter fullscreen modeExit fullscreen mode

Start activity B again:

A -> B -> C -> D -> E -> B
Enter fullscreen modeExit fullscreen mode

2. Single top

In this launch mode, if an activity is at the top of the task and you create its instance again, then a new instance will not be created. InsteadonNewIntent() will be called with updated data. If activity is not on top, a new instance will be pushed. See with examples:

The task with activities:

A -> B -> C -> D
Enter fullscreen modeExit fullscreen mode

Launch activity B. As B is not on top, a new instance will be created:

A -> B -> C -> D -> B
Enter fullscreen modeExit fullscreen mode

Launch activity B again. As B is on top,onNewIntent() on B will be called:

A -> B -> C -> D -> B
Enter fullscreen modeExit fullscreen mode

3. Single task

In this launch mode, if the activity doesn't exist in the task, a new instance is created otherwiseonNewIntent() is called. Additionally, activities above it get destroyed. Let's understand this with examples:

The task with activities:

A -> B -> C -> D
Enter fullscreen modeExit fullscreen mode

Start activity E with launch mode single task:

A -> B -> C -> D -> E
Enter fullscreen modeExit fullscreen mode

Start activity B with launch mode single task:

A -> B
Enter fullscreen modeExit fullscreen mode

Now you can see that B is already there. So a new instance is not created. TheonNewIntent() of B is called. Also, C, D, and E which are above B were destroyed.

4. Single instance

For an activity that has a single instance launch mode, a new task is created. First, see the example, then we can explain it.

The task with activities:

A -> B -> C -> D
Enter fullscreen modeExit fullscreen mode

Launch E with launch mode single instance:

Task-1: A -> B -> C -> D
Enter fullscreen modeExit fullscreen mode
Task-2: E
Enter fullscreen modeExit fullscreen mode

If you launch E again, it will not create a new task. In the same instance of E, theonNewIntent() method will be called.

How to add launch modes

Launch mode can be set fromAndroidManifest.xml like this:

<activityandroid:name=”.MainActivity”android:launchMode=”singleTop”/>
Enter fullscreen modeExit fullscreen mode

or Java/Kotlin code using flags like this:

valintent=Intent(activity,HomeGenericActivity::class.java).apply{addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)}
Enter fullscreen modeExit fullscreen mode

See, that's very easy!!! 😁😁😁 I hope you enjoyed reading this article.

Top comments(4)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
arthacker8209 profile image
Deepak Kumawat
  • Joined

Very Informative@mohitrajput987 Thanks for sharing...

CollapseExpand
 
mohitrajput987 profile image
Mohit Rajput
Android apps and SDK engineer | Kotlin | MVVM, MVP, Clean Swift architectures | Technical blogger
  • Email
  • Location
    Bangaluru, India
  • Education
    B.Tech - Computer Science & Engineering
  • Work
    Android Tech Lead
  • Joined

Thanks Deepak

CollapseExpand
 
luffy31 profile image
Raj
  • Joined

Great Article!

CollapseExpand
 
mehul_garg_09b3879c220685 profile image
Mehul Garg
  • Joined

Easy to understand and nicely explained via examples and diagrams@mohitrajput987

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Android apps and SDK engineer | Kotlin | MVVM, MVP, Clean Swift architectures | Technical blogger
  • Location
    Bangaluru, India
  • Education
    B.Tech - Computer Science & Engineering
  • Work
    Android Tech Lead
  • Joined

More fromMohit Rajput

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp