How to Save an Activity State Using the Save Instance State in Android

How to Save an Activity State Using the Save Instance State in Android

22 May 2024 Stephan Petzl Leave a comment Tech-Help

When developing applications on the Android SDK platform, it’s crucial to understand how to save an application’s state. This process ensures that your app can restore its state seamlessly after being paused or stopped. Below, we provide a comprehensive guide on how to achieve this using the onSaveInstanceState and onRestoreInstanceState methods.

Understanding the Basics

The onSaveInstanceState method is called before an activity may be killed so that when it comes back sometime in the future, it can restore its state. Conversely, the onRestoreInstanceState method is called to retrieve the saved state when the activity is recreated.

Example Implementation

Consider the following simplified example of saving and restoring an activity state:

package com.android.hello;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class HelloAndroid extends Activity {

  private TextView mTextView = null;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mTextView = new TextView(this);

    if (savedInstanceState == null) {
       mTextView.setText("Welcome to HelloAndroid!");
    } else {
       mTextView.setText("Welcome back.");
    }

    setContentView(mTextView);
  }

  @Override
  public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putString("MyString", "Welcome back to Android");
  }

  @Override
  public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    String myString = savedInstanceState.getString("MyString");
    mTextView.setText(myString);
  }
}

Step-by-Step Guide

1. Saving State

To save the state, override the onSaveInstanceState method and write the application state values you want to save to the Bundle parameter:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);
  savedInstanceState.putBoolean("MyBoolean", true);
  savedInstanceState.putDouble("myDouble", 1.9);
  savedInstanceState.putInt("MyInt", 1);
  savedInstanceState.putString("MyString", "Welcome back to Android");
}

2. Restoring State

To restore the state, override the onRestoreInstanceState method and read the values from the Bundle:

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
  double myDouble = savedInstanceState.getDouble("myDouble");
  int myInt = savedInstanceState.getInt("MyInt");
  String myString = savedInstanceState.getString("MyString");
}

Best Practices

  • Always call the superclass methods super.onSaveInstanceState and super.onRestoreInstanceState to ensure the default behavior is preserved.
  • Use onSaveInstanceState for transient state data that you need to restore if the activity is temporarily destroyed and recreated.
  • For more persistent data that needs to survive application restarts, consider using SharedPreferences or SQLite databases.

Additional Resources

For more detailed examples and advanced techniques, you can refer to the following documentation:

Streamlining Your Mobile Testing with Repeato

Managing application states and ensuring seamless user experiences can be complex. This is where tools like Repeato come into play. Repeato is a no-code test automation tool for iOS and Android, designed to help you create, run, and maintain automated tests for your apps efficiently.

With Repeato, you can focus on developing your application while delegating test automation to non-technical colleagues or QAs. Its computer vision and AI-based approach make it particularly fast to edit and run tests, ensuring that your app maintains high quality without the overhead of manual testing.

Explore more about how Repeato can enhance your mobile development workflow on our blog.

Like this article? there’s more where that came from!