Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Restore Android Activity state

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

    We know about the common life cycle methods of Activity but what happens when user rotates screen : Basically whenever user rotates the screen android save the states in onSaveInstanceState() and recreate the activity by getting all view state from onCreate() bundle or from the onRestoreInstanceState() method.

    onSaveInstanceState() method runs after onResume() and before onStop() but it doesn't guarantee to run before or after onPause.

    onRestoreInstanceState() method runs after onStart() method of lifecycle.

    Example :

        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            mScore = savedInstanceState.getInt(SCORE);
            mLevel = savedInstanceState.getInt(LEVEL);
        } else {
            // initialize members with default values
        }}
    

    and using onRestoreInstanceState() :

    public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    
    mScore = savedInstanceState.getInt(SCORE);
    mLevel = savedInstanceState.getInt(LEVEL);}
    

 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: