Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine fragment restored from backstack

Been searching for this issue for a while to no avail now:

How to determine fragment is being restored from backstack? I'm using the compatibility library and a ListFragment inside a FragmentActivity. When an item inside ListFragment is selected, a new Fragment is started to replace the ListFragment.

I noticed that when the FragmentActivity gets paused, the Fragment's onSaveInstanceState is called. But when the Fragment is put into the back stack via FragmentTransaction, onSaveInstanceState doesn't get called, then the lifecycle methods onCreateView and onActivityCreated gets called with null savedInstanceState Bundle.

I'm asking this because I want to load some data when the Fragment is created or restored, but not so when user comes back via. backstack.

I've looked at How to check if Fragment was restored from a backstack? but want to add more details in hopes this would incite an answer.

Edit: just noticed http://developer.android.com/reference/android/app/Fragment.html#onSaveInstanceState(android.os.Bundle) says

Note however: this method may be called at any time before onDestroy(). There are many situations where a fragment may be mostly torn down (such as when placed on the back stack with no UI showing), but its state will not be saved until its owning activity actually needs to save its state.

So onSaveInstanceState is definitely out of the question...

like image 284
dvd Avatar asked Oct 27 '11 17:10

dvd


People also ask

How do you know if a fragment is recreated?

Nevertheless, here is a quick and easy tip on how you can determine if your onViewCreated() gets called from a recreation: As you can see we check if the savedInstanceState null . If that is the case, we know the Fragment had no previous state and therefore is fully recreated.

What is Backstack in fragments?

Calling addToBackStack() commits the transaction to the back stack. The user can later reverse the transaction and bring back the previous fragment by pressing the Back button. If you added or removed multiple fragments within a single transaction, all of those operations are undone when the back stack is popped.

How can I maintain fragment state when added to the back stack?

Various Android system operations can affect the state of your fragment. To ensure the user's state is saved, the Android framework automatically saves and restores the fragments and the back stack. Therefore, you need to ensure that any data in your fragment is saved and restored as well.


2 Answers

I think that most simple way is do this for example in onViewCreated() method:

if (savedInstanceState == null && !mAlreadyLoaded) {     mAlreadyLoaded = true;      // Do this code only first time, not after rotation or reuse fragment from backstack } 

Because when android put fragment on backstack, it only destroy its view, but don't kill instance itself, so mAlreadyLoaded will be still true when fragment will be restored from backstack.

like image 103
ATom Avatar answered Oct 02 '22 13:10

ATom


getSupportFragmentManager().addOnBackStackChangedListener(new OnBackStackChangedListener() {                 public void onBackStackChanged() {                 Log.i(TAG, "back stack changed ");                 int backCount = getSupportFragmentManager().getBackStackEntryCount();                 if (backCount == 0){                                    // block where back has been pressed. since backstack is zero.                 }             }         }); 

use this addOnBackStackChangedListener.

like image 34
Yashwanth Kumar Avatar answered Oct 02 '22 14:10

Yashwanth Kumar