Android Activity Launch Mode Standard SingleTop Example

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

1.Standard Launch Mode.

  1. This is the default launch mode, you can also specify it in the 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>
  2. If an activity is configured with this launch mode, when the user requests a new activity instance, the android os will create a new one and put the new one at the top of the back stack.
  3. The below diagram shows the standard launch mode activity, you need to type the back menu three times to close all opened activities.
    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 the Logcat console and displayed in the TextView object.
  3. When clicking the button, it will send an intent to start a new instance of the same activity. You can see this step demo video above or on the youtube URL https://youtu.be/OynW9ZKRnk4

1.2 Main Layout Xml File.

  1. 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.

  1. 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. SingleTop Launch Mode.

If you can not watch the above video, you can see it on the youtube URL https://youtu.be/6tqjuwLDWig

  1. If you configured the activity with the singleTop launch mode, when you send an intent to android os to get a new instance of such activity, the 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.
  2. 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>
  3. 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);
                }
            });
        }
    }
  4. 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>
  5. 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);
                }
            });
        }
    }
  6. You can read the article Android Activity Launch Mode SingleTask SingleInstance Example to learn the other two activity launch modes.

Leave a Comment

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.