michael
michael

Reputation: 110570

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?

Upvotes: 42

Views: 34970

Answers (6)

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 partandroid: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.

Upvotes: -2

Gaspar
Gaspar

Reputation: 159

Follow these steps (Using Android Studio):

  1. Create New Logcat Filter, e.g. 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.

Upvotes: 4

Theo
Theo

Reputation: 6083

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/

Upvotes: 2

chaitanya
chaitanya

Reputation: 1601

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.

Upvotes: 18

Goran Horia Mihail
Goran Horia Mihail

Reputation: 3645

From doc:

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

Upvotes: 18

SteD
SteD

Reputation: 14027

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

Upvotes: 7

Related Questions