Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where can be use onSaveInstanceState where parameters are passed different Activity?

For Acitivity there is already a method called onSaveInstacestate(Bundle) which used to store the data of activity which is overridden method.

As i see, there are two different onSaveInstanceState where parameters are passed differently as below.

@Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState)
    {
        super.onSaveInstanceState(outState, outPersistentState);
        Log.i("test", "onSaveInstanceState called **********");
    }

and

@Override
    protected void onSaveInstanceState(Bundle outState)
    {
         super.onSaveInstanceState(outState);
          Log.i("test", "onSaveInstanceState with bundle only called");
    }

So, at what circumstances this two methods can be use? please describe in detail. Thanks in advance.

like image 485
Swift Avatar asked May 25 '16 05:05

Swift


2 Answers

This deserves an expanded answer. As the accepted answer states, since API level 21 there's an additional overload for onSaveInstanteState.

Available since API level 1 (Docs):

void onSaveInstanceState (Bundle outState)

Addition introduced with API level 21 (Docs):

void onSaveInstanceState (Bundle outState, PersistableBundle outPersistentState)

The latter one with the PersistableBundle is not a replacement of the former one. It is only used when the Activity attribute R.attr.persistableMode is set to as persistAcrossReboots. When such an Activity is going to be persisted, onSaveInstanceState (Bundle outState, PersistableBundle outPersistentState) will be called and you receive a PersistableBundle to store your instance state.

To restore the state of an Activity with R.attr.persistableMode set to as persistAcrossReboots, there's

void onRestoreInstanceState (Bundle savedInstanceState, PersistableBundle persistentState)

Be aware that onRestoreInstanceState (Bundle savedInstanceState) is not called if the one with the PersistableBundle is called. I assume the same is true for onSaveInstanceState, but I haven't checked it and the docs do not mention it at the time of API level 28.

There's also an appropriate overload for onCreate().

like image 104
aha Avatar answered Sep 21 '22 07:09

aha


From API Level 21, onSaveInstanceState()has a new parameter called that takes object of PersistableBundle. You can read more about PersistableBundle on Docs

In short,

For API 21 and above

@Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState)
    {
        super.onSaveInstanceState(outState, outPersistentState);
        Log.i("test", "onSaveInstanceState called **********");
    }

For API less than 20

@Override
    protected void onSaveInstanceState(Bundle outState)
    {
         super.onSaveInstanceState(outState);
          Log.i("test", "onSaveInstanceState with bundle only called");
    }
like image 30
onkar Avatar answered Sep 22 '22 07:09

onkar