Android Save Retrieve Activity Instance State Example

When an android activity stopped, it maybe recycled by android OS. But how to store recycled activity instance state data, and use those activity instance data later? Android provide below methods.

1. How To Save / Retrieve Activity Instance State Data.

android save activity state example

  1. Override Activity’s onSaveInstanceState(Bundle outState) method. And save the desired data in the Bundle input parameter with key value pair.
  2. The onSaveInstanceState(Bundle outState) method will be invoked before onStop() method.
  3. Retrieve the saved instance state data in onCreate(Bundle savedInstanceState) method use the Bundle input parameter also.
  4. You can also retrieve the saved instance state data in onRestoreInstanceState(Bundle savedInstanceState) method.
  5. Please Note: The activity instance state data can only be saved and retrieved between different instances of same activity class. Different activity class can not use this method to share data.

2. Save / Retrieve Activity Instance State Data Example.

2.1 Example Activities.

This example contains two activity.

  1. ActivityStateActivity : This activity includes a input text box and a button. User can input email in the input text box, when click the button, it will open below activity.
  2. ActivityStateTargetActivity : This activity contains a TextView which show some text message.

2.2 Example Execution Steps.

  1. Click the button in first activity. The second activity will be displayed.
    android save retrieve activity instance state data open second activity
  2. Click emulator tool bar to change the screen orientation from vertical to horizontal. This can make the first activity be destroyed and recycled by android OS.
    android change activity screen orientation from vertical to horizontal
  3. Click the back menu at the bottom of the emulator.
  4. You can see the input email still exist.
    android activity instance state example first activity
  5. But please see the log data in LogCat, when return back, the first activity’s onCreate() method is called again.
    android activity instance state example logcat log data
  6. If you do not change the screen orientation from vertical to horizontal. From the log, we can see the first activity’s onRestart() method will be invoked automatically. But this time, the first activity is not destroyed and recycled. It is still the original activity.
    android save retrieve activity instance state data not change activity screen orientation
    android save retrieve activity instance state data not change screen orientation logcat log data
READ :   Android Activity Example - Switch Between Screens

2.3 Main Layout Xml File.

activity_state.xml

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

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Email : "/>
        <EditText
            android:id="@+id/emailInputBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <Button
        android:id="@+id/startAnotherActivityButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start Another Activity"/>
</LinearLayout>

2.4 Main Activity Java File.

ActivityStateActivity.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.EditText;

public class ActivityStateActivity extends AppCompatActivity {

    // Log tag name.
    private final static String LOG_TAG_UI = "LOG_TAG_UI";

    // Bundle state key.
    private final static String USER_INPUTTED_EMAIL = "USER_INPUTTED_EMAIL";

    // Email input text box.
    private EditText emailInputBoxEditText = null;

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

        emailInputBoxEditText = (EditText)findViewById(R.id.emailInputBox);

        if(savedInstanceState!=null)
        {
            Log.d(LOG_TAG_UI, "Main activity onCreate savedInstanceState is not null.");
            // Retrieve activity instance state data.
            String email = savedInstanceState.getString(USER_INPUTTED_EMAIL);
            // Set the original email data in EditText view component.
            emailInputBoxEditText.setText(email);
        }else
        {
            Log.d(LOG_TAG_UI, "Main activity onCreate savedInstanceState is null.");
        }

        setTitle("dev2qa.com - Activity State Example");

        // Click this button to display second activity.
        Button startAnotherActivityButton = (Button)findViewById(R.id.startAnotherActivityButton);
        startAnotherActivityButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ActivityStateActivity.this, ActivityStateTargetActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(LOG_TAG_UI, "Main activity onRestart.");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(LOG_TAG_UI, "Main activity onPause.");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(LOG_TAG_UI, "Main activity onStop.");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(LOG_TAG_UI, "Main activity onDestroy.");
    }

    // This method will be invoked before onStop() method.
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        String email = emailInputBoxEditText.getText().toString();
        outState.putString(USER_INPUTTED_EMAIL, email);
        Log.d(LOG_TAG_UI, "Main activity onSaveInstanceState.");
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        String email = savedInstanceState.getString(USER_INPUTTED_EMAIL);
        Log.d(LOG_TAG_UI, "Main activity onRestoreInstanceState.");
    }
}

2.5 Target Activity Layout Xml File.

This xml file also include another button. Click this button will finish and destroy all activities in this task stack.

READ :   Android Frequently Asked Questions

activity_state_target.xml

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="This is the target activity."
        android:textSize="20dp"/>

    <Button
        android:id="@+id/destroyPreviousActivityButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Destroy Previous Activity"/>
</LinearLayout>

2.6 Target Activity Java File.

ActivityStateTargetActivity.java

package com.dev2qa.example;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.dev2qa.example.constant.LogTagName;

import java.util.List;

public class ActivityStateTargetActivity extends AppCompatActivity {

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

        setTitle("dev2qa.com - Target Activity.");

        // Click this button will destroy and recycle all activities.
        Button destroyPreviousActivityButton = (Button)findViewById(R.id.destroyPreviousActivityButton);
        destroyPreviousActivityButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Context context = getApplicationContext();

                // Get activity manager.
                ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

                // Get AppTask list.
                List<ActivityManager.AppTask> appTaskList = activityManager.getAppTasks();

                if(appTaskList!=null)
                {
                    // Loop the list, destroy and recycle all activity in this activity stack.
                    int appTaskListSize = appTaskList.size();
                    for(int i = 0;i<appTaskListSize;i++)
                    {
                        ActivityManager.AppTask appTask = appTaskList.get(i);
                        ActivityManager.RecentTaskInfo recentTaskInfo = appTask.getTaskInfo();

                        ComponentName componentName = recentTaskInfo.baseActivity;

                        String baseActivityClassName = componentName.getClassName();

                        Log.d(LogTagName.LOG_TAG_UI, baseActivityClassName);

                        if("com.dev2qa.example.ActivityStateActivity".equals(baseActivityClassName))
                        {
                            appTask.finishAndRemoveTask();
                        }
                    }
                }
            }
        });
    }
}
(Visited 972 times, 1 visits today)

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.