0

I am storing data to the "outState" in this method:

@Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putInt("my_int", mValue);

    }

This means, that when the activity is restarted, in the onCreate() method, I can do:

if (null != savedInstanceState) {

savedInstanceState.getInt("my_int");
}

However, this leads to a problem after the screen is switched off. When the screen is switched off, the activity is killed. This means that onSaveInstanceState() is not called if rotation occurs during the screen off period and the savedInstancestate is null because the activity has been completely restarted. As a result, my value has not been saved and restored even though the device has been rotated.

How do I get around this?

  • This is not exactly true. Android documentation clearly says that onSaveInstanceState(Bundle) is always called before Activity is placed in a background ("killable") state and this really works well. The issue should be somewhere else. Look for "In addition, the method onSaveInstanceState(Bundle)" http://developer.android.com/reference/android/app/Activity.html – sergej shafarenka Jan 19 '14 at 17:50
  • @beworker `savedInstanceState` is still null after the activity has been recreated though, so even if it is actually saving, I am unable to resore it. –  Jan 19 '14 at 17:57
  • If you create new empty project with a single activity, override onSaveInstanceState() and store value there. Then launch this project in a newly created official Android emulator, lock the emulator (F7) then rotate emulator (F11), then unlock screen, I am quite sure your activity will call onCreate() with savedInstanceState != null. Can you confirm this? – sergej shafarenka Jan 19 '14 at 18:03
  • @RiThBo what you said can never happen, screen off doesn't mean that application killed. It just went to background that's it so there is no effect on method calls. Make sure you are not calling `finish()` anywhere in the activity when it is going to background. – Piyush Agarwal Jan 19 '14 at 18:11
  • @beworker @pyus I just made another project to test this. `onSaveInstanceState()` I save a value. Then `onCreate()`, if `savedInstanceSave != null`, I create a toast. What happens is this: When rotated, the value is saved, onCreate is called and the toast shows. However, if I rotate with the screen off, then screen on, the toast does show. –  Jan 19 '14 at 18:20
  • @beworker screen rotation doesn't happen while your phone is locked. so nothing will be called. Do one thing, rotate your screen when it is off and unlock the device in rotated screen than method will be called. – Piyush Agarwal Jan 19 '14 at 18:23
  • @pyus13 I just proved that savedInstanceState is null after rotating the screen while the screen is off. –  Jan 19 '14 at 18:30
  • let me know the device and os version you are using, Also which API level you are using to build the app. – Piyush Agarwal Jan 19 '14 at 18:43
  • @pyus13 Nexus 4, Android 4.4.2, compiling with 4.4.2 –  Jan 19 '14 at 20:00
  • @RiThBo I have tested in many devices it is working fine. Please check my answer once. I have posted because it is not good to have long discussion on question itself. So later I will update in answer. – Piyush Agarwal Jan 20 '14 at 10:32

1 Answers1

0

Few things you have to check

  1. Your application will not even rotate if the screen is in locked or in off state so nothing will be called.(It happens because how application/activity can rotate if it in background, same like home key press)

  2. Make sure you are not calling finish anywhere in the app when it goes in background.

  3. Also check, is your app getting crash when you keep it in background ?

Press home button while app is running and bring it in to front using recent apps screen. Check if onCreate() is calling again. If it is than your app is crashing or finishing when it goes to background.

I have tested in variety of devices almost 10 different, including Sony Xperia Z, Samsung Galaxy s3 , Nexus 4(as mentioned in comments)) and it is working fine.

Piyush Agarwal
  • 25,608
  • 8
  • 98
  • 111
  • I did some more testing on my app and you are correct that `onCreate()` is being called when the screen locks. I am not calling `finish()` anywhere in my code, so I don't know why I am having a problem. Do you have any ideas? –  Jan 20 '14 at 20:47
  • I cant help you without seeing your code. Please include you Activity code Activity registration in AndroidManifest.xml file. – Piyush Agarwal Jan 20 '14 at 20:57
  • I have over 2000 lines in that activity. I try and post some of it later. –  Jan 21 '14 at 06:27
  • 1
    This much LOC, i suggest you to break your code in classes. 2k lines in a single class is not a best practice. – Piyush Agarwal Jan 21 '14 at 06:54
  • 1
    sorry for taken a while to return. I have found I was mistaken. After performing more thorough testing, savedInstanceState is not null after the screen turns off and rotates. There must be a bug somewhere in my code. Thanks for your help, I will accept your answer as you were very helpful. –  Jan 22 '14 at 16:57