Android Activity Launch Mode Standard SingleTop Example

An activity can be configured with four launch mode, each launch mode is used in one special scenario. This article will introduce all the four launch mode of activity with examples.

1. Launch Mode Standard.

This is the default launch mode, you can also specify it in AndroidManifest.xml file like below.

<activity android:name=".ActivityLaunchModeFirstActivity"
    android:launchMode="standard">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

If an activity is configured with this launch mode, when user request a new activity instance, android os will create a new one and put the new one at the top of the back stack.

Below diagram shows the standard launch mode activity, you need to type back menu three time to close all opened activity.

android activity standard launch mode

1.1 Activity Standard Launch Mode Code Example.

  1. This example contains a TextView and a Button.
  2. The activity belongs task id and activity string format value are both printed in logcat console and displayed in the TextView object.
  3. When click the button, it will send an intent to start a new instance of  same activity.android activity standard launch mode example

1.2 Main Layout Xml File.

activity_launch_mode_first.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/firstActivityIdTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"/>

    <Button
        android:id="@+id/startFirstActivityButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start First Activity"/>

</LinearLayout>

1.3 Activity Java File.

ActivityLaunchModeFirstActivity.java

package com.dev2qa.example;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ActivityLaunchModeFirstActivity extends AppCompatActivity {

    private static final String TAG = "ACTIVITY_ID";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch_mode_first);

        // Create Task and activity related id info.
        StringBuffer strBuf = new StringBuffer();
        strBuf.append("Task Id : ");
        strBuf.append(this.getTaskId());
        strBuf.append(" , Activity Id : ");
        strBuf.append(this.toString());

        // Set above info in TextView object.
        TextView firstActivityIdTextView = (TextView)findViewById(R.id.firstActivityIdTextView);
        firstActivityIdTextView.setText(strBuf.toString());

        // Log above info in logcat console.
        Log.d(TAG, strBuf.toString());

        // When click this button, start a new instance of current activity.
        Button startFirstActivityButton = (Button)findViewById(R.id.startFirstActivityButton);
        startFirstActivityButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ActivityLaunchModeFirstActivity.this, ActivityLaunchModeFirstActivity.class);
                startActivity(intent);
            }
        });
    }
}

2. Launch Mode SingleTop.

If you configured the activity with singleTop launch mode, when you send an intent to android os to get a new instance of such activity, android os will first check the top activity in the back stack, if it is the requested activity type, it will return it, otherwise it will create a new instance of requested activity and return.

READ :   Android CardView With Image And Text Example

android activity singletop launch mode example

android activity singletop launch mode one activity

android activity singletop launch mode two activity

activity_launch_mode_first.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/firstActivityIdTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"/>

    <Button
        android:id="@+id/startFirstActivityButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start First Activity"/>

    <Button
        android:id="@+id/startSecondActivityButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start Second Activity"/>

</LinearLayout>

ActivityLaunchModeFirstActivity.java

package com.dev2qa.example;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ActivityLaunchModeFirstActivity extends AppCompatActivity {

    private static final String TAG = "ACTIVITY_ID";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch_mode_first);

        // Create Task and activity related id info.
        StringBuffer strBuf = new StringBuffer();
        strBuf.append("Task Id : ");
        strBuf.append(this.getTaskId());
        strBuf.append(" , Activity Id : ");
        strBuf.append(this.toString());

        // Set above info in TextView object.
        TextView firstActivityIdTextView = (TextView)findViewById(R.id.firstActivityIdTextView);
        firstActivityIdTextView.setText(strBuf.toString());

        // Log above info in logcat console.
        Log.d(TAG, strBuf.toString());

        // When click this button, start a new instance of current activity.
        Button startFirstActivityButton = (Button)findViewById(R.id.startFirstActivityButton);
        startFirstActivityButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ActivityLaunchModeFirstActivity.this, ActivityLaunchModeFirstActivity.class);
                startActivity(intent);
            }
        });

        // When click this button, start a new instance of second activity.
        Button startSecondActivityButton = (Button)findViewById(R.id.startSecondActivityButton);
        startSecondActivityButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ActivityLaunchModeFirstActivity.this, ActivityLaunchModeSecondActivity.class);
                startActivity(intent);
            }
        });
    }
}

activity_launch_mode_second.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/secondActivityIdTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"/>

    <Button
        android:id="@+id/startFirstActivityFromSecondActivityButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start First Activity"/>

</LinearLayout>

ActivityLaunchModeSecondActivity.java

package com.dev2qa.example;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ActivityLaunchModeSecondActivity extends AppCompatActivity {

    private static final String TAG = "ACTIVITY_ID";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch_mode_second);

        // Create Task and activity related id info.
        StringBuffer strBuf = new StringBuffer();
        strBuf.append("Task Id : ");
        strBuf.append(this.getTaskId());
        strBuf.append(" , Activity Id : ");
        strBuf.append(this.toString());

        // Set above info in TextView object.
        TextView secondActivityIdTextView = (TextView)findViewById(R.id.secondActivityIdTextView);
        secondActivityIdTextView.setText(strBuf.toString());

        // Log above info in logcat console.
        Log.d(TAG, strBuf.toString());

        // When click this button, start a new instance of First activity.
        Button startFirstActivityFromSecondActivityButton = (Button)findViewById(R.id.startFirstActivityFromSecondActivityButton);
        startFirstActivityFromSecondActivityButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ActivityLaunchModeSecondActivity.this, ActivityLaunchModeFirstActivity.class);
                startActivity(intent);
            }
        });
    }
}

You can read article Android Activity Launch Mode SingleTask SingleInstance Example to learn other two activity launch mode.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.