简体   繁体   中英

Why onRestoreInstanceState() never gets called

I am trying to save data in my activity and than restore it. I save data in on onSaveInstanceState() and then I try to restore the data in onRestoreInstanceState() .

I setup breakpoint, the method onSaveInstanceState() get called. But onRestoreInstanceState() or onCreate() never did.

Here is my steps:

  1. start my Activity .
  2. press 'Home' button on the phone. onSaveInstanceState() get called.
  3. Click the icon in the launcher and launch my Activity again.

At this time, only onRestart() get called. But not onRestoreInstanceState() or onCreate() .

Does anyone know why?

From doc:

The system calls onRestoreInstanceState() only if there is a saved state to restore.

Well, if onRestart() is called, the value of the instance variables would be maintained by the application stack itself and thus you do not need to restore them.

onCreate() method is only called when your Activity 's onStop() is called and the process is killed.

Please refer the Activity life cycle Android Activity Life Cycle for a clear understanding.

You may want to check whether the onStop() method is called and if your process is killed. I do no think that your process gets killed by the scenario which you have described.

the onRestoreInstanceState() method is very tricky. I do not know when exactly it is called but I saw it was called once while changing from Potrait to Landscape.

I have asked similiar question earlier on here

Here's some steps to test out onRestoreInstanceState() :

  1. Press home screen
  2. Kill the app through adb
  3. Launch your app again

Follow these steps (Using Android Studio):

  1. Create New Logcat Filter, eg AppState
  2. Launch the app on your emulator. You will see:

    I/AppState﹕ onCreate

    I/AppState﹕ onStart

    I/AppState﹕ onResume

  3. Press Ctl-F12 to rotate the emulator. You will see:

    I/StateChange﹕ onPause

    I/StateChange﹕ onSaveInstanceState

    I/StateChange﹕ onStop

    I/StateChange﹕ onDestroy

    I/StateChange﹕ onCreate

    I/StateChange﹕ onStart

    I/StateChange﹕ onRestoreInstanceState

    I/StateChange﹕ onResume

This causes the destruction and recreation of the activity by making a configuration change to the device, such as rotating from portrait to landscape.

See the link below for how to test onSaveInstanceState() and onRestoreInstanceState() on a real device or in the emulator.

This method uses the AlwaysFinish setting, which is simpler and faster than killing processes. This method also provides Activity -level control rather than process level control:

http://bricolsoftconsulting.com/how-to-test-onsaveinstancestate-and-onrestoreinstancestate-on-a-real-device/

This is my solution for real device so onRestoreInstanceState get it called.

  1. in manifest on related activity remove this part android:configChanges="orientation"
  2. in manifest on related activity remove this part android:screenOrientation="portrait"
  3. in your activity remove this line if it's there setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  4. on your device enable rotating from setting - display - auto rotate.

then run your app, rotate your device. that's it.

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