Shubham
Shubham

Reputation: 3163

Showing the same fragment on restoring the app from background

Say for instance I have Activity A, which has a FrameLayout that can hold one fragment at a time. Lets say that when the Activity is first started it loads Fragment A into the FrameLayout, which consists of a ListView. When an item is selected in Fragment A it starts up Fragment B.Clicking a item on Fragment B takes you to the Fragment C. The way I am currently doing this is by simply hiding Fragment A and then adding Fragment B since this preserves Fragment A's state. I am of course also adding this fragment transaction to the backstack.

So now Fragment A exists on the backstack. Say now I go back to my Android home screen and go to another app. While I am doing this, the Android system decides to destroy my application because the system needs more memory. When I navigate back to my application, how am I supposed to properly restore the same fragment where user left the app.So if user was on Fragment C when he minimised the app,and when he reopens it from minimised app Fragment A loads up.

Upvotes: 2

Views: 1912

Answers (1)

natario
natario

Reputation: 25194

You can try to override onSaveInstanceState() and onRestoreInstanceState() methods in your activity (see here ).

static final String LAST_FRAGMENT = "userFragment";
...

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putInt(LAST_FRAGMENT, mCurrentFragment);
    super.onSaveInstanceState(savedInstanceState);
}

Then, when creating,

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
       if (savedInstanceState != null) {
        mCurrentFragment = savedInstanceState.getInt(LAST_FRAGMENT);
        }
       else { mCurrentFragment = mDefaultFragment }
       ...

       //then you need to load mCurrentFragment

Here mCurrentFragment is an instance of (the name of, or whatever) last fragment used. You can keep this value updated by overriding the onPause() method of the fragment, for example. So each fragment's onPause() has to set mCurrentFragment to a new value identifying the fragment itself.

Another way to do it might be using SharedPreference. In each fragment's onPause(), set:

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
sp.edit().putInt(LAST_FRAGMENT, $int_identifying_your_fragment ).apply();

Then in your activity onCreate,

static final String LAST_FRAGMENT = "userFragment";
static int mCurrentFragment;
static int mDefaultFragment = 0;
...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
    mCurrentFragment = sp.getInt(LAST_FRAGMENT, mDefaultFragment);
    ...
    /* then you load desired fragment according to mCurrentFragment
    if (mCurrentFragment == 1), load fragment1 and so on */

I've used ints here, but you can take any reference of your fragments.

Upvotes: 2

Related Questions