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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 143
    Comment on it

    If you need to save state of an activity then you have to @override onSaveInstanceState(Bundle savedInstanceState) method of activity and put some states values that you want change to the Bundle parameter as below:

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
      super.onSaveInstanceState(savedInstanceState);
      // Save UI state values to the savedInstanceState.
      // This bundle will be passed to onCreate if the process is
      // killed and restarted.
      savedInstanceState.putString("StringValue", "Hey!");
      savedInstanceState.putBoolean("isBoolean", true);
      savedInstanceState.putInt("isInteger", 1);
    
      // etc.
    }
    

    Bundle used for passing data between various activities of android. Bundle can hold all type of values, but it depends on you that what type of values you want to pass. Bundle is way to storing the values in NVP ("Name-Value Pair") map, and it will get passed in to onCreate() method of activity and also onRestoreInstanceState() where you'd extract the values as below:

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
      super.onRestoreInstanceState(savedInstanceState);
      // Restore UI state from the savedInstanceState.
      // Bundle has also been passed to onCreate.
      boolean myBoolean = savedInstanceState.getBoolean("isBoolean");
      int myInt = savedInstanceState.getInt("isInteger");
      String myString = savedInstanceState.getString("StringValue");
    }
    

    // This is the basic technique to store instance for you application .

 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: