1

I'm trying to restore the previous state of my Activity after pressing home and reopening the app so I'm saving what I need inside onSaveInstanceState:

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

        outState.putInt(STATE_ACTIVE_VIEW_ID, mActiveViewId);

        Log.v(TAG, this.toString() + " onSaveInstanceState");

    }

that is called, but inside onCreate the Bundle is always null.

This is OnCreate():

@Override
public void onCreate(Bundle inState) {
    super.onCreate(inState);
    setContentView(R.layout.main);

    setMenuDrawer();


    if (inState != null) {

        int activeViewID = inState.getInt(STATE_ACTIVE_VIEW_ID);
        View activeView = findViewById(activeViewID);
        menuDrawer.setActiveView(activeView);
        switchSelectedFragment(activeView);
        Log.v(TAG, this.toString() + " onCreate " + inState.toString());


    } else {
        switchSelectedFragment(findViewById(R.id.homeView));

    }

    Log.v(TAG, this.toString() + " onCreate");

}
brescia123
  • 517
  • 6
  • 23
  • 1
    onRestoreInstanceState(bundle) and onSaveInstanceState(bundle) is used to save the state during activity orientation change. Using shared prefrences would help your case. – Raghunandan Apr 23 '13 at 14:36
  • so the bundle created inside the onSaveInstanceState call after the home press is not the one read by the following onCreate? – brescia123 Apr 23 '13 at 15:00
  • Your expectations are correct, however a few things can affect this behavior. Do you have any flags set on your `Activity` in the manifest or when it is started? Also, post your `menuDrawer.saveState()` and `onCreate()` methods. – Steven Byle Apr 23 '13 at 17:12
  • modified the post with onCreate and remove outState.putParcelable(STATE_MENUDRAWER, menuDrawer.saveState()); because it is ininfluent – brescia123 Apr 23 '13 at 20:21
  • @Raghunandan will u please help me in this http://stackoverflow.com/questions/20903545/how-to-call-framelayout-slider-in-diffrent-activties-in-android – Pooja Dubey Jan 07 '14 at 05:01
  • @Raghunandan on this question also i need u r help .please help me http://stackoverflow.com/questions/20835135/set-text-on-a-gridview-image-in-android – Pooja Dubey Jan 07 '14 at 05:02

2 Answers2

1

onSaveInstanceState is called when the home button is pressed, but onCreate will not be called again unless the Activity was destroyed (config change or memory reasons).

You should simply be able to hold onto the member variable, for dealing with onPause/onResume (which is what we're actually discussing). If you're not seeing the Fragment you'd expect (after returning from Home press), you have a different issue.

Paul Burke
  • 25,496
  • 9
  • 66
  • 62
  • So how it is possible that pressing the home button and pressing the app icon I see the call of on create? – brescia123 Apr 24 '13 at 09:33
  • @brescia123 That shouldn't happen, and means you are doing something wrong. Perhaps, in onPause? – Paul Burke Apr 24 '13 at 12:43
  • It should also be noted, that if you are using Fragments properly, you shouldn't need a transaction to restore the Fragment when the savedInstanceState is not null. – Paul Burke Apr 24 '13 at 14:20
-1

You have to do all the saving work in onPause() method, preferably using sharedPreferences as Raghunandan said.

Neoh
  • 15,906
  • 14
  • 66
  • 78