Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Use SaveInstanceState to Save & Restore Android Activity State?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 42
    Comment on it

    The Android activity state is used for Saving and restoring activity state. On App creation, we have scenarios in which activity is either destroyed by pressing the Back button or by calling the finish() method.

     

     

    To save and restore your activity state below are two overridden methods explained with example, one is, onSaveInstanceState() for storing activity state in a Bundle and the other one is onRestoreInstanceState() to restore the key-value pairs stored in a Bundle object.

    Let's see both one by one:

     

    1. Save your activity state

    If you wish to save your activity state using Save Instance for that you have to override onSaveInstanceState(Bundle saveInstanceState) in your activity and write the activity state values that you wish to save with parameter making use of Bundle.

    See example below:-

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
      // Always call the superclass so it can save the view hierarchy state
      super.onSaveInstanceState(savedInstanceState);
    
      // Save UI state changes to the savedInstanceState bundle, this bundle will be passed to onCreate if the process is killed or restarted.
      // save user current state values
      savedInstanceState.putBoolean("BooleanValue", false);
      savedInstanceState.putDouble("DoubleValue", 1.9);
      savedInstanceState.putInt("IntValue", 1);
      savedInstanceState.putString("StringValue", "hi");
      
    }

     

    2. Restore your activity state

    Now, as soon as you will kill the activity, all the data mentioned within overridden method will get saved and now when the activity is reopened then we will retrieve the same data by overriding another method named onRestoreInstanceState(Bundle savedInstanceState) in the same activity.

    See example below:-

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
      // Always call the superclass so it can restore the view hierarchy
      super.onRestoreInstanceState(savedInstanceState);
      
      // Restore UI state from the savedInstanceState, This bundle has also been passed to onCreate.
      // Restore state members from saved instance
      boolean booleanValue = savedInstanceState.getBoolean("BooleanValue");
      double doubleValue = savedInstanceState.getDouble("DoubleValue");
      int intValue = savedInstanceState.getInt("IntValue");
      String stringValue = savedInstanceState.getString("StringValue");
    }

     

    Note: To restore the view hierarchy always call super class first by super.onRestoreInstanceState().

    That's all about saving and restoring activity state for now, for any inputs or questions, feel free to write in comment section below.

    How to Use SaveInstanceState to Save & Restore Android Activity State?

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: