3

I've wrote all the needed code to save my Activity state (a simple form with EditText widgets) and restore it on phone rotation and it works great.

My problem is on managing screen on/off changes: when going off, onSaveInstanceState is called and everything goes well. On screen on, onRestoreInstanceState is not called, neither onCreate, only onResume.

Am I missing something?

Geob-o-matic
  • 5,940
  • 4
  • 35
  • 41

3 Answers3

14

The onRestoreInstanceState method is called only if the activity process is killed(due to memory constraints or some other reasons) and then restored when it becomes visible again. On the phone rotation, the activity is killed and restored with different orientation so it will work. But on screen on/off it wouldn't be the case. The activity has not been destroyed so no need for restore

bala
  • 415
  • 3
  • 13
  • what about if user kills the activity ? such as with back button press ? will onrestoreInstanceState get called when user navigates to activity again ? – j2emanue May 27 '18 at 11:41
  • 1
    @j2emanue No the `onrestoreInstanceState()` method is not called when the user presses the back button, also note that the `onSaveInstanceState()` will not be called when the user presses the back button – Dharmaraj May 11 '20 at 05:11
-1

Its also called while zoom pressing (for example on Tablet).

Vaid
  • 1
-1

onRestoreInstanceState is called to save the state of the activity before being killed. When the screen goes off, the activity is put in a pause state - because the keyguard is an activity that come in front of your activity. to catch the screen off/on cycle, to have to override onPause / onResume methods, as stated in http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

andijcr
  • 427
  • 3
  • 13
  • 2
    I believe you meant 'onSaveInstanceState' instead of 'onRestoreInstanceState' (at the beginning of your answer). – Leeeeeeelo Oct 24 '12 at 08:25