onRestoreInstanceState and onSaveInstanceState (Activity creation and restoration)

The role of onSaveInstanceState() method:

  • Save the corresponding instance state (referring to key-value pairs) in the Bundle object, so that when the activity is re-created, you can restore to its previous state by obtaining the value stored in the bundle

The role of onRestoreInstanceState() method

  • Get the instance state stored in the bundle, and restore the activity to the state we expect through the stored value

When to call the two methods:

Insert picture description here

  • The call timing of onSaveInstanceState() is 1 in the above figure : the
    current activity enters the background from the Resumed state (fully visible state), or the current activity is recycled by the system, the system will call onSaveInstanceState( after calling the onStop() method ) Method, such as jumping to other activity or clicking Home, but if you press the back key or call the finish() function in the code, then the activity cannot be restored, so the onSaveInstanceState() method will not be called to save the corresponding information.

  • The call timing of onRestoreInstanceState() is 2 in the above figure :
    When the system recycles our activity and restores it again, it will call the onStart() method after the Created state, and then call the onRestoreInstanceState() method, such as when the screen is rotated Will call onRestoreInstanceState(). But the activity does not call onRestoreInstanceState() when it enters the background and becomes visible. The system will only call onRestoreInstanceState() when there is state information that needs to be restored. Therefore, in the onRestoreInstanceState() method, we don’t need to determine whether the bundle is null. This also means that the onRestoreInstanceState() method must also be called when onRestoreInstanceState() is called. Called. But if the data in the Bundle is restored in onCreate(), then we need to consider whether the Bundle is null.

Code practice:


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class Main2Activity extends AppCompatActivity {
    
    
    private static final String TAG = "Main2Activity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Log.d(TAG, "onCreate: ");
    }

    @Override
    protected void onStart() {
    
    
        super.onStart();
        Log.d(TAG, "onStart: ");
    }
    
    @Override
    protected void onResume() {
    
    
        super.onResume();
        Log.d(TAG, "onResume: ");
    }
    
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
    
    
        // 去恢复父类的相应的层次结构
        super.onRestoreInstanceState(savedInstanceState);
        Log.d(TAG, "onRestoreInstanceState: ");
    }

    @Override
    protected void onStop() {
    
    
        super.onStop();
        Log.d(TAG, "onStop: ");
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
    
    
        Log.d(TAG, "onSaveInstanceState: ");
        // 存储相应的层次结构
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onDestroy() {
    
    
        super.onDestroy();
        Log.d(TAG, "onDestroy: ");
    }
}
  • Back to home output:

2019-12-07 14:39:15.695 2928-2928/com.example.lxd.fragmentlearn D/Main2Activity: onCreate:
2019-12-07 14:39:15.702 2928-2928/com.example.lxd.fragmentlearn D/Main2Activity: onStart:
2019-12-07 14:39:15.706 2928-2928/com.example.lxd.fragmentlearn D/Main2Activity: onResume:
2019-12-07 14:39:20.502 2928-2928/com.example.lxd.fragmentlearn D/Main2Activity: onStop:
2019-12-07 14:39:20.503 2928-2928/com.example.lxd.fragmentlearn D/Main2Activity: onSaveInstanceState:

You can see that onSaveInstanceState is called after the onStop() method is called.

  • Click the back button to output

2019-12-07 14:40:11.914 3414-3414/? D/Main2Activity: onCreate:
2019-12-07 14:40:11.921 3414-3414/? D/Main2Activity: onStart:
2019-12-07 14:40:11.926 3414-3414/? D/Main2Activity: onResume:
2019-12-07 14:40:14.080 3414-3414/com.example.lxd.fragmentlearn D/Main2Activity: onStop:
2019-12-07 14:40:14.086 3414-3414/com.example.lxd.fragmentlearn D/Main2Activity: onDestroy:

And onSaveInstanceState is not called.

At the same time, for the above two cases, the onRestoreInstanceState method is not executed when the activity is opened.

  • Rotate the screen

2019-12-07 14:44:27.636 5123-5123/com.example.lxd.fragmentlearn D/Main2Activity: onCreate:
2019-12-07 14:44:27.642 5123-5123/com.example.lxd.fragmentlearn D/Main2Activity: onStart:
2019-12-07 14:44:27.647 5123-5123/com.example.lxd.fragmentlearn D/Main2Activity: onResume:
2019-12-07 14:44:30.394 5123-5123/com.example.lxd.fragmentlearn D/Main2Activity: onStop:
2019-12-07 14:44:30.394 5123-5123/com.example.lxd.fragmentlearn D/Main2Activity: onSaveInstanceState:
2019-12-07 14:44:30.397 5123-5123/com.example.lxd.fragmentlearn D/Main2Activity: onDestroy:
2019-12-07 14:44:30.615 5123-5123/com.example.lxd.fragmentlearn D/Main2Activity: onCreate:
2019-12-07 14:44:30.620 5123-5123/com.example.lxd.fragmentlearn D/Main2Activity: onStart:
2019-12-07 14:44:30.623 5123-5123/com.example.lxd.fragmentlearn D/Main2Activity: onRestoreInstanceState:
2019-12-07 14:44:30.625 5123-5123/com.example.lxd.fragmentlearn D/Main2Activity: onResume:

Rotating the screen will call the onRestoreInstanceState method, and before the onRestoreInstanceState method is called, the onSaveInstanceSate method is called, but this example does not indicate that the system must call the onSaveInstanceState method before onRestoreInstanceSate. If you know the verification method, thank you for letting me know.

Guess you like

Origin blog.csdn.net/liu_12345_liu/article/details/103434783