Search this site
Embedded Files
Squirrel of Rama
  • Home
  • Institute Coaching Programs
  • Android curriculum
    • Android Topics
      • Notification
      • Broadcast & Broadcast Receiver
      • Content Providers
      • Activities
        • Activity and Lifecycle
        • Saving state
        • Tasks and the back stack
      • Services
        • Services
        • Foreground Service
        • Bound Service
      • Dependency Injection
      • Workmanager
      • Fragment Findings*
      • Navigation findings*
      • Permissions Findings*
      • Room & Datastore findings*
    • Kotlin Topics
      • Nullables
      • Exceptions
      • Conditions and Loops
      • Arrays
      • Collections
      • Collection Functions
      • Class & Object
      • OOP Concepts
      • Scope Function
      • Visibility Modifiers
      • Generics
      • Kotlin Classes Types
        • Enums
        • Sealed Class
        • Data Classes
        • Nested and inner classes
        • Object expressions and declarations
      • Kotlin Functions Types
        • Functions
        • Extension functions
        • Higher-order functions and lambdas
      • Couroutines
      • Kotlin Flows
    • Interview Questions
      • Android Interview Questions
  • Products
    • Shabri
    • WedExhibit
  • About
Squirrel of Rama
  • Home
  • Institute Coaching Programs
  • Android curriculum
    • Android Topics
      • Notification
      • Broadcast & Broadcast Receiver
      • Content Providers
      • Activities
        • Activity and Lifecycle
        • Saving state
        • Tasks and the back stack
      • Services
        • Services
        • Foreground Service
        • Bound Service
      • Dependency Injection
      • Workmanager
      • Fragment Findings*
      • Navigation findings*
      • Permissions Findings*
      • Room & Datastore findings*
    • Kotlin Topics
      • Nullables
      • Exceptions
      • Conditions and Loops
      • Arrays
      • Collections
      • Collection Functions
      • Class & Object
      • OOP Concepts
      • Scope Function
      • Visibility Modifiers
      • Generics
      • Kotlin Classes Types
        • Enums
        • Sealed Class
        • Data Classes
        • Nested and inner classes
        • Object expressions and declarations
      • Kotlin Functions Types
        • Functions
        • Extension functions
        • Higher-order functions and lambdas
      • Couroutines
      • Kotlin Flows
    • Interview Questions
      • Android Interview Questions
  • Products
    • Shabri
    • WedExhibit
  • About
  • More
    • Home
    • Institute Coaching Programs
    • Android curriculum
      • Android Topics
        • Notification
        • Broadcast & Broadcast Receiver
        • Content Providers
        • Activities
          • Activity and Lifecycle
          • Saving state
          • Tasks and the back stack
        • Services
          • Services
          • Foreground Service
          • Bound Service
        • Dependency Injection
        • Workmanager
        • Fragment Findings*
        • Navigation findings*
        • Permissions Findings*
        • Room & Datastore findings*
      • Kotlin Topics
        • Nullables
        • Exceptions
        • Conditions and Loops
        • Arrays
        • Collections
        • Collection Functions
        • Class & Object
        • OOP Concepts
        • Scope Function
        • Visibility Modifiers
        • Generics
        • Kotlin Classes Types
          • Enums
          • Sealed Class
          • Data Classes
          • Nested and inner classes
          • Object expressions and declarations
        • Kotlin Functions Types
          • Functions
          • Extension functions
          • Higher-order functions and lambdas
        • Couroutines
        • Kotlin Flows
      • Interview Questions
        • Android Interview Questions
    • Products
      • Shabri
      • WedExhibit
    • About

Tasks and the back stack ↗

Define launch modes
Define launch modes using the manifest file
singleTop
singleTask
singleInstance
singleInstancePerTask
Define lauch modes using intent flags

A task is a collection of activities that users interact with when trying to do something in your app. These activities are arranged in a stack called the back stack in the order in which each activity is opened.

Define launch modes

Launch modes let you define how a new instance of an activity is associated with the current task. You can define launch modes in two ways, described in the sections that follow:

  • Using the manifest file
    When you declare an activity in your manifest file, you can specify how the activity associates with tasks when it starts.

  • Using intent flags
    When you call startActivity(), you can include a flag in the Intent that declares how (or whether) the new activity associates with the current task.

So, if Activity A starts Activity B, Activity B can define in its manifest how it associates with the current task, and Activity A can use an intent flag to request how Activity B can associate with current task.

If both activities define how Activity B associates with a task, then Activity A's request, as defined in the intent, is honored over Activity B's request, as defined in its manifest.

Note: Some launch modes available for the manifest file aren't available as flags for an intent. Likewise, some launch modes available as flags for an intent can't be defined in the manifest.


Define launch modes using the manifest file

When declaring an activity in your manifest file, you can specify how the activity associates with a task using the <activity> element's launchMode attribute.

  1. standard

The default mode. The system creates a new instance of the activity in the task it was started from and routes the intent to it. The activity can be instantiated multiple times, each instance can belong to different tasks, and one task can have multiple instances.

  1. singleTop

If an instance of the activity already exists at the top of the current task, the system routes the intent to that instance through a call to its onNewIntent() method, rather than creating a new instance of the activity.

For example, suppose a task's back stack consists of root activity A with activities B, C, and D on top (so the stack is A-B-C-D, with D on top). An intent arrives for an activity of type D. If D has the default "standard" launch mode, a new instance of the class is launched, and the stack becomes A-B-C-D-D. However, if D's launch mode is "singleTop", the existing instance of D receives the intent through onNewIntent(), because it's at the top of the stack, and the stack remains A-B-C-D. If, on the other hand, an intent arrives for an activity of type B, then a new instance of B is added to the stack even if its launch mode is "singleTop".

  1. singleTask

The system creates the activity at the root of a new task or locates the activity on an existing task with the same affinity. If an instance of the activity already exists, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance. Meanwhile all of the other activities on top of it are destroyed.

Activities with the singleTask launch mode can be part of a task that contains other activities.


Example use cases include home screens, main dashboards, or login screens where you want to ensure only one instance exists in the back stack.

  1. singleInstance

The behavior is the same as for "singleTask", except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task. Any activities started by this one open in a separate task.

  • Activities with the singleInstance launch mode run in a separate task and are always at the root of that task. They cannot be launched as part of a task with other activities.

  • This mode is typically used for global singleton activities, such as main menus or settings screens, where you want to ensure only one instance exists regardless of where it's launched from.

  • It's also useful for handling intents from external sources, such as deep links, where you want only one instance of the activity to handle all incoming intents.

  • The singleInstance launch mode is the most restrictive. It creates a new task to host the activity and allows only one instance of the activity to exist in the entire system.

In summary, singleTask allows multiple instances of the activity within a task but ensures only one instance within a particular task, while        singleInstance ensures only one instance of the activity exists in the entire system, running in its own separate task.

  1. singleInstancePerTask

Confusing


Note: "singleTask" remove all activities that are above the starting activity from the task. For example, suppose a task consists of root activity A with activities B and C. The task is A-B-C, with C on top. An intent arrives for an activity of type A. If A's launch mode is "singleTask" or "singleInstancePerTask", the existing instance of A receives the intent through onNewIntent(). B and C are finished, and the task is now A.

Define lauch modes using intent flags

A brand by Jagrut Soni
Google Sites
Report abuse
Page details
Page updated
Google Sites
Report abuse