Understand Activity Launch Mode With Examples


This article is in the continuation of my article Android Activity Launch Mode. Before jumping into the example I would recommend to go through the basic concepts of Activity Launch Mode in my previous article.
I have created different sample projects for each launch mode. You can download it from github.

Create a new Android project and define activities named ActivityA, ActivityB, ActivityC and ActivityD in your application manifest. Define launch mode = singleTop for ActivityD.


 <?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.androidjavapoint.singletop">



    <application

        android:allowBackup="true"

        android:icon="@mipmap/ic_launcher"

        android:label="@string/app_name"

        android:supportsRtl="true"

        android:theme="@style/AppTheme">

        <activity android:name=".ActivityA">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />



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

            </intent-filter>

        </activity>

        <activity android:name=".ActivityB" />

        <activity android:name=".ActivityC" />

        <activity

            android:name=".ActivityD"

            android:launchMode="singleTop" />



    </application>



</manifest>




Create layout files for each activity and put it in /res/layout folder.


<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context="com.androidjavapoint.singletop.ActivityA">



    <TextView

        android:id="@+id/textLifeCycle"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:gravity="center_horizontal"

        android:text="@string/activity_a"

        android:textSize="18dp"

        android:textStyle="bold" />



    <TextView

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_above="@+id/buttonGoToB"

        android:gravity="center_horizontal"

        android:text="@string/activity_review"

        android:textSize="18dp"

        android:textStyle="italic|bold" />



    <Button

        android:id="@+id/buttonGoToB"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_alignParentBottom="true"

        android:layout_marginTop="10dp"

        android:text="@string/goToB" />

</RelativeLayout>
 
Create activity classes as declared in the manifest. Implement View.OnClickListener in the Activity to listen callback for button onClick event.

package com.androidjavapoint.singletop;


import android.content.Intent;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.TextView;



public class ActivityA extends AppCompatActivity implements View.OnClickListener {



    private String TAG = this.getClass().getSimpleName();

    private StringBuilder mCalledLifeCycleMethods;

    private TextView mCalledLifeCycleMethodsText;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_a);



        mCalledLifeCycleMethods = new StringBuilder();



        mCalledLifeCycleMethodsText = (TextView) findViewById(R.id.textLifeCycle);



        findViewById(R.id.buttonGoToB).setOnClickListener(this);



        updateLifeCycleMethod("Called OnCreate()");

    }



    @Override

    protected void onNewIntent(Intent intent) {

        super.onNewIntent(intent);

        updateLifeCycleMethod("Called onNewIntent()");

    }



    @Override

    protected void onPause() {

        super.onPause();

        updateLifeCycleMethod("Called onPause()");

    }



    @Override

    protected void onResume() {

        super.onResume();

        updateLifeCycleMethod("Called onResume()");

    }



    @Override

    protected void onDestroy() {

        super.onDestroy();

        updateLifeCycleMethod("Called onDestroy()");

    }



    @Override

    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.buttonGoToB:

                startActivity(new Intent(this, ActivityB.class));

                break;

        }

    }



    /**

     * Update the text view

     *

     * @param method activity life cycle method name

     */

    private void updateLifeCycleMethod(String method) {

        mCalledLifeCycleMethods.append(TAG).append(" : ").append(method).append("\n");

        mCalledLifeCycleMethodsText.setText(mCalledLifeCycleMethods.toString());

    }

}


Let’s try to understand android activity launch mode in detail with above sample application.

android:launchMode="standard"

Start Activity B with standard mode. A new instance of Activity B in the task will be created.




Below diagram explain that if Activity B already has one instance then to start the Activity B again will create new instance of it on the same task.


android:launchMode="singleTop"

Start Activity D with singleTop mode. A new instance of Activity D in the task will be created.


Below diagram explain that if Activity D already has one instance then to start the Activity D again will not create  new  instance for it rather system will forward call via onNewIntent() method.



android:launchMode="singleTask"
Start Activity B with singleTask mode. A new task will be created and a new instance will be created.


Below diagram explain that if Activity B already has one instance then to start the Activity B again will not create  new  instance for it rather system will forward call via onNewIntent() method. Here you can observe that Activity B has cleared activity record on top of it and is now root of the task.



android:launchMode="singleInstance"

Start Activity D with standard mode. A new instance will be created in different task.


Below diagram explain that if Activity D starts the Activity E then it will be created in the previous task. Activity D will always remain root Activity of the task.



Below diagram explain that if Activity D already has one instance then to start the Activity D again (In our case stared again from Activity E) will not create  new  instance for it rather system will forward call via onNewIntent() method.




Hope above article clears the concept of different activity launch mode and its association with tasks.

To find more interesting topics on Software development follow me at https://medium.com/@ankit.sinhal


You can also find my Android Applications on play store



Comments

Popular posts from this blog

Android Performance: Avoid using ENUM on Android

Android O: Impact On Running Apps And Developer Viewpoint

Smart way to update RecyclerView using DiffUtil