42

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?

Sufian
  • 6,405
  • 16
  • 66
  • 120
michael
  • 106,540
  • 116
  • 246
  • 346

6 Answers6

18

From doc:

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

user2864740
  • 60,010
  • 15
  • 145
  • 220
Goran Horia Mihail
  • 3,536
  • 2
  • 29
  • 40
  • 11
    Why would onSaveInstanceState() be called if onRestoreInstanceState() wasn't going to ever be called? It's crazy. If I switch to another activity the onSaveInstanceState() being called makes me think that I'll be able to access a saved instance state when I come back. But it never hits any of the restore/onCreate functions because it's restarting. It's poorly explained in documentation as well. – Matthew Bahr Jun 21 '17 at 00:06
18

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.

Sufian
  • 6,405
  • 16
  • 66
  • 120
chaitanya
  • 1,591
  • 2
  • 24
  • 39
  • http://stackoverflow.com/questions/4967435/onrestoreinstancestate-not-called-when-screen-wake-up - there's an answer what onRestoreInstanceState is for. – jellyfish May 13 '11 at 14:37
  • 4
    Your answer is not exactly accurate or it's at least unclear, because after onStop() either onRestart() is going to be called or onDestroy(). if onDestroy() is called then you're back to onCreate() – LuxuryMode Jan 11 '12 at 19:28
  • Thanks. onRestore... seems to be called when orientation is changed. Simplified things for me no end (+: – Everyone Nov 09 '12 at 18:08
7

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
Community
  • 1
  • 1
SteD
  • 13,909
  • 12
  • 65
  • 76
4

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.

Gaspar
  • 159
  • 7
2

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/

Sufian
  • 6,405
  • 16
  • 66
  • 120
Theo
  • 5,963
  • 3
  • 38
  • 56
-2

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.