简体   繁体   中英

What does (savedInstanceState) mean

In my main activity there is some code which says

if (savedInstanceState != null) {
            mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
            mFromSavedInstanceState = true;

and I don't quite understand what the (savedInstanceState) means in this code. Can someone tell me what it means in this context because I'm sort of a noob and I want to understand the code so that I can learn it, understand it and even replicate it when needed.

Quoted from ( http://developer.android.com/training/basics/activity-lifecycle/recreating.html ):

There are a few scenarios in which your activity is destroyed due to normal app behavior, such as when the user presses the Back button or your activity signals its own destruction by calling finish(). The system may also destroy your activity if it's currently stopped and hasn't been used in a long time or the foreground activity requires more resources so the system must shut down background processes to recover memory.

Further, If you save the state of the application in a bundle (typically non-persistent, dynamic data in onSaveInstanceState), it can be passed back to onCreate if the activity needs to be recreated (eg, orientation change) so that you don't lose this prior information. If no data was supplied, savedInstanceState is null.

If your Activity is in the background, the Android OS may decide to kill the process that contains this Activity if memory is low. To handle this possibility, you can persist the state of the Activity in a Bundle.

If you go back to the Activity that was killed, the persisted state will be passed to onCreate of the Activity in the savedInstanceState parameter. This will allow you to restore the Activity to its last state.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM