Reputation: 110570
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:
Activity
.onSaveInstanceState()
get called.Activity
again. At this time, only onRestart()
get called. But not onRestoreInstanceState()
or onCreate()
.
Does anyone know why?
Upvotes: 42
Views: 34970
Reputation: 4247
This is my solution for real device so onRestoreInstanceState get it called.
manifest
on related activity remove this part android:configChanges="orientation"
manifest
on related activity remove this partandroid:screenOrientation="portrait"
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
then run your app, rotate your device. that's it.
Upvotes: -2
Reputation: 159
Follow these steps (Using Android Studio):
Launch the app on your emulator. You will see:
I/AppState﹕ onCreate
I/AppState﹕ onStart
I/AppState﹕ onResume
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
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:
Upvotes: 2
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
Reputation: 3645
From doc:
The system calls onRestoreInstanceState() only if there is a saved state to restore.
Upvotes: 18