Android save state on orientation change

2 min read 07-10-2024
Android save state on orientation change


Keeping Your Android App State Alive: Mastering Orientation Changes

Have you ever noticed that your Android app resets its state when you rotate your device? It's a common problem that can lead to frustrating user experiences, especially when users have invested time filling out forms or navigating complex menus. This article will guide you through the process of preserving your app's state gracefully when the screen orientation changes.

The Problem: Losing State During Orientation Change

Imagine a user filling out a lengthy form in your app. They've carefully entered their information, and just as they're about to submit, they rotate their device. Suddenly, the form is wiped clean! This is a prime example of why managing state during orientation changes is crucial.

The culprit: By default, Android destroys and recreates the activity when orientation changes, resulting in lost data.

Example:

public class MyActivity extends AppCompatActivity {
  private EditText inputField;

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

    inputField = findViewById(R.id.input_field);
  }
}

In this simple example, when the orientation changes, onCreate() is called again, effectively resetting inputField and losing any text entered by the user.

The Solution: Bundles to the Rescue!

Android provides a handy tool called a Bundle to save and restore your app's state. Think of it as a temporary storage container for your data.

Step 1: Saving State in onSaveInstanceState()

@Override
protected void onSaveInstanceState(Bundle outState) {
  super.onSaveInstanceState(outState);
  outState.putString("input_text", inputField.getText().toString()); 
}

Here, we capture the current text from inputField and store it in the outState Bundle using the key "input_text".

Step 2: Restoring State in onCreate()

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

  inputField = findViewById(R.id.input_field);
  if (savedInstanceState != null) {
    inputField.setText(savedInstanceState.getString("input_text"));
  }
}

Now, if savedInstanceState is not null (meaning the activity was recreated), we restore the saved text into inputField.

Advanced Techniques for State Management

While the Bundle approach works well for simple data, for complex scenarios, consider these alternatives:

  • ViewModel: This architectural component helps manage UI-related data in a lifecycle-aware manner, ensuring state persistence across configuration changes.
  • SharedPreferences: Use this to store key-value pairs persistently, suitable for basic settings or user preferences.

Important Note: Always handle onSaveInstanceState() and onCreate(Bundle savedInstanceState) consistently to maintain state integrity.

Conclusion

Saving your app's state during orientation changes is essential for a smooth user experience. By using Bundle, ViewModel, or SharedPreferences, you can ensure that your users' progress and preferences are preserved even when the screen configuration changes. Remember to choose the right approach based on your app's complexity and the specific data you need to save.