When your code executes the following line:
startActivityForResult(photoPickerIntent, PHOTO_SELECT_REQUEST_CODE);
Your activity will receive onPause() , onStop() , but not onDestroy() every time. If the android system is not working at this time, your activity can get onDestroy()
To maintain fragment status and activity and prevent leakage:
Save state in onSaveInstanceState(Bundle)
In situations where the system needs more memory, it can kill paused processes (after onPause() ) to restore resources. Because of this, you must be sure that all your state will be saved by the time you return from this function. Typically, onSaveInstanceState(Bundle) used to store the state of each instance in activity, and this method is used to store global persistent data (in content providers, files, etc.).
Restore your state in onRestoreInstanceState(Bundle)
onRestoreInstanceState(Bundle) method is called after onStart() when the activity is reinitialized from the previously saved state specified here in savedInstanceState . Most implementations simply use onCreate(Bundle) to restore their state, but sometimes itβs convenient to do it here after initialization is completed or to allow subclasses to decide whether to use your default implementation. By default, the implementation of this method restores any view state that was previously frozen using onSaveInstanceState(Bundle) .
It can help you fulfill your requirement.
source share