Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to save activity instance state

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 245
    Comment on it

    Every android activity follow a life-cycle. There are certain situation when an activity goes into pause state or in stopped state but at some particular time your application need to keep valuable state (i.e. data)

    This we can achieve by using the callback method known as onSaveInstanceState( ) as follows:-

    Here is the code snippet for storing the activity state:-

    Step 1:- to store the values:-

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
      super.onSaveInstanceState(savedInstanceState);
    
     /***This bundle will be passed to onCreate if the process is killed and restarted.***/
      savedInstanceState.putBoolean("booleanValue", true);
      savedInstanceState.putDouble("doubleValue", 1.9);
      savedInstanceState.putInt("integerValue", 1);
      savedInstanceState.putString("stringValue", "Getting Back");
    }
    

    Step 2:- to get the stored values:-

     @Override
        public void onRestoreInstanceState(Bundle savedInstanceState) {
          super.onRestoreInstanceState(savedInstanceState);
          // This bundle has also been passed to onCreate.
          boolean storedBooleanValue = savedInstanceState.getBoolean("booleanValue");
          double storedDoubleValue = savedInstanceState.getDouble("doubleValue");
          int storedIntegerValue = savedInstanceState.getInt("integerValue");
          String storedStringValue = savedInstanceState.getString("stringValue");
        }
    

 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: