Android Tutorial

Software Setup and Configuration

Android Studio

File Structure

Components

Core Topics

Layout

View

Button

Intent and Intent Filters

Toast

RecyclerView

Fragments

Adapters

Other UI Component

Image Loading Libraries

Date and Time

Material Design

Bars

Working with Google Maps

Chart

Animation

Database

Advance Android

Jetpack

Architecture

App Publish

App Monetization

How to Restore Data on Configuration Changed in Android using Bundles?

In Android, activities can be destroyed and recreated when a configuration change occurs (e.g., screen rotation). This can lead to the loss of temporary data. To prevent this, Android provides a mechanism to save and restore such data using Bundle.

Here's how you can restore data during configuration changes using Bundle:

1. Save Data on Configuration Change:

Override the onSaveInstanceState method in your Activity:

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    
    // Save your data to the bundle
    outState.putString("key_string", "Your String Data");
    outState.putInt("key_integer", 123);
    // ... add more data as required
}

2. Restore Data After Configuration Change:

There are two ways to restore the saved data:

  • Using the onRestoreInstanceState method:

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        
        // Restore saved data from the bundle
        String savedString = savedInstanceState.getString("key_string");
        int savedInt = savedInstanceState.getInt("key_integer");
        // ... get more data as required
    }
    
  • Directly from the onCreate method:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        if (savedInstanceState != null) {
            // Restore saved data from the bundle
            String savedString = savedInstanceState.getString("key_string");
            int savedInt = savedInstanceState.getInt("key_integer");
            // ... get more data as required
        }
    }
    

3. Handle Configuration Changes Manually (Optional):

If you want to handle configuration changes manually without destroying and recreating the activity, you can specify so in the AndroidManifest.xml. However, use this option with caution, as it requires you to handle all aspects of the configuration change yourself:

<activity android:name=".YourActivity"
          android:configChanges="orientation|screenSize">
</activity>

Then, override the onConfigurationChanged method in your activity:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Handle configuration change
    // For instance, you can change layouts or resources based on orientation
}

Note: Using android:configChanges is not recommended in most situations because handling all configuration changes can be complex. The preferred approach is to save and restore instance state using Bundle, allowing the system to destroy and recreate your activity.

  1. Android Bundle savedInstanceState example:

    • Below is an example of saving and restoring a simple data type:
    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        outState.putString("key", "Hello, saved data!");
        super.onSaveInstanceState(outState);
    }
    
    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        String savedData = savedInstanceState.getString("key");
        // Use the restored data
    }
    
  2. Saving and restoring data with Bundles in Android activity lifecycle:

    • The Android activity lifecycle includes events such as onSaveInstanceState, onCreate, and onRestoreInstanceState. Use Bundles to manage data during these events.
    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        outState.putInt("counter", counter);
        super.onSaveInstanceState(outState);
    }
    
    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        counter = savedInstanceState.getInt("counter");
        // Use the restored data
    }