Вопрос

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?

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top