X

    Get a Quote

    Launch modes in Android

    Launch Modes In Android

    1,781 views
    Amit Shukla

    To understand the Launch modes in Android you need to understand Task And Back Stack first

    Task And Back Stack

    TASK: — A Task is a stack of activity i.e. a Task is a group of activities arranged in a Stack.
    Back Stack: — Order of Activities in which they are opened reside in a stack called Back stack.

    task and back stack

    Launch Modes:-

    • Launch Modes in Android define how a new or the existing instance of an activity is associated with the current task
    • Launch modes in Android can also be set through Flag in Intent
    • Or Declare in Manifest file

    <activity android:launch mode = [“standard” | “singleton” | “single-task” | “single instance”] ../>

    There are four types of launch Modes in Android:

    1. Standard
    2. Single Top
    3. Single task
    4. Single Instance

    Also Read : List Of Unique Android App Ideas for Your Next Business

    Standard Launch Mode:

    • By default, android launches activities in standard mode.
    • It always creates a new instance of an activity. Without taking into consideration that the stack already has an instance of that activity, whenever the onCreate() method of an activity is called its new instance is created.
    • Works with FILO(First In Last Out)

    standard lauch mode

    Single Top Launch Mode

    • In this, if the instance of an activity is already present in the stack then it does not create another instance of that activity rather than reuse the same i.e. It will route to the same Activity (only if that activity is on top of the stack) else it will create a new instance.
    • Single top launch mode routes Activity by “New Intent ()”.

    single-top launch mode

    Single Task Launch Mode

    Single Task can be with Affinity and Without Affinity

    To understand Single task Launch Mode in Android you need to understand “task affinity” first.

    TASK AFFINITY:

    • Task affinity means a single application can have more than one stack
    • You can give any name to stack (android:taskaffinity=” com.some.task.affinity).
    • E.g, There are two stack

    Stack A which is named like the package name

    Stack B which is named as com.some.task.affinity 2 task

    <activity android name:”.Activity_b” android:launch mode:”singleTask” android:taskaffinity=”com.some.task.affinity”/>

    Single Task With Task Affinity:

    • There can be only  one activity that we can declare as “single-task” across a stack, but there can be more than one activity in an application that can be declared as a single task but with a different task affinity
    • Whenever an activity is launched it will always share the Affinity of its root activity

    Example:

    Activity B is declared as “single-task”

    <activity android name:”.Activity_b” android:launch mode:”single-task” android:taskaffinity=”com.some.task.affinity”/>

    Step 1:  Activity B is launched so open in another stack

    singleTaskstep process

    Step 2: Activity C is launch, it will share the affinity of root activity so it will open in stack 2

    singleTaskstep process

    Step 3: Activity D is launch, it will share the affinity of root activity so it will open in stack 2

    singleTaskstep process

    Step 4: If Activity B is launch again, then Activity C and Activity D will be pop, and Activity B will be reused.

    Launch modes in Android

    • With on click of a recent button, both stacks go in the background, so whichever stack is selected, will become the current stack, and the activity launched will be open in that current stack.

    Single Task without Task Affinity:

    • The instance of an activity is reused.
    • All Activities above the launched activity will be removed from the stack

    Launch modes in Android

    Single Instance Launch Mode

    • There is no need to declare Affinity in the manifest file
    • Whenever an activity is declared as “Single Instance”, then that particular activity will automatically open in another stack.
    • New activities don’t share the affinity of root activity
    • <activity android name:”.Activity_b” android:launch mode:”single instance” />

    Forex

    In the given fig,

    Activity B is declared as a single Instance as launch mode so it automatically opens in another stack(Stack B) and all activity launched before and after activity 1, opens in the same stack(Stack A).

    singleInstance

    By Saloni Agarwal

    Avatar for Amit
    The Author
    Amit Shukla
    Director of NBT
    Amit Shukla is the Director of Next Big Technology, a leading IT consulting company. With a profound passion for staying updated on the latest trends and technologies across various domains, Amit is a dedicated entrepreneur in the IT sector. He takes it upon himself to enlighten his audience with the most current market trends and innovations. His commitment to keeping the industry informed is a testament to his role as a visionary leader in the world of technology.