简体   繁体   中英

savedInstanceState null after returning to activity

so I have read a ton on the subject both here on SO and else where on the web (eg https://sites.google.com/site/jalcomputing/home/mac-osx-android-programming-tutorial/saving-instance-state ). But I still don't get it. So here is my scenario. CatActivity is running. Then I pull the Drawer by clicking icon in action bar and from the drawer select DogActivity. From DogActivity I start ChickenActivity from which I in turn start CatActivity. When I return to CatActivity, my savedInstanceState is null. Before I left, I did this:

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putLong("important value", data);
    System.out.println("testing: " + data);
    super.onSaveInstanceState(outState);
}

But when I return onRestoreInstanceState is not called. So I tried the manual in onCreate :

if (savedInstanceState != null) {
    data = savedInstanceState.getLong("important value");
    System.out.println("important value returned: " + data);
}

But here it was null.

Any input on how to fix this?

Try swapping your super call:

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putLong("important value", data);
    System.out.println("testing: " + data);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM