Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragments onResume from back stack

I'm using the compatibility package to use Fragments with Android 2.2. When using fragments, and adding transitions between them to the backstack, I'd like to achieve the same behavior of onResume of an activity, i.e., whenever a fragment is brought to "foreground" (visible to the user) after poping out of the backstack, I'd like some kind of callback to be activated within the fragment (to perform certain changes on a shared UI resource, for instance).

I saw that there is no built in callback within the fragment framework. is there s a good practice in order to achieve this?

like image 540
oriharel Avatar asked Jun 28 '11 07:06

oriharel


People also ask

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.

What is fragment back stack in Android?

If you add one Fragment into the back stack, when you press the android device back menu, you can find the Fragment that is saved in the back stack popup. Until all the saved Fragments in the back stack popup, then the activity will exit.

How do you resume an existing Backstack fragment?

Since you want only one back stack entry per Fragment , make the back state name the Fragment's class name (via getClass(). getName() ). Then when replacing a Fragment , use the popBackStackImmediate() method. If it returns true, it means there is an instance of the Fragment in the back stack.

How do you refresh a fragment on a resume?

The onResume() get called always before the fragment is displayed. Even when the fragment is created for the first time . So you can simply move your from onViewCreated() to onResume .


1 Answers

For a lack of a better solution, I got this working for me: Assume I have 1 activity (MyActivity) and few fragments that replaces each other (only one is visible at a time).

In MyActivity, add this listener:

getSupportFragmentManager().addOnBackStackChangedListener(getListener()); 

(As you can see I'm using the compatibility package).

getListener implementation:

private OnBackStackChangedListener getListener()     {         OnBackStackChangedListener result = new OnBackStackChangedListener()         {             public void onBackStackChanged()              {                                    FragmentManager manager = getSupportFragmentManager();                  if (manager != null)                 {                     MyFragment currFrag = (MyFragment) manager.findFragmentById(R.id.fragmentItem);                      currFrag.onFragmentResume();                 }                                }         };          return result;     } 

MyFragment.onFragmentResume() will be called after a "Back" is pressed. few caveats though:

  1. It assumes you added all transactions to the backstack (using FragmentTransaction.addToBackStack())
  2. It will be activated upon each stack change (you can store other stuff in the back stack such as animation) so you might get multiple calls for the same instance of fragment.
like image 144
oriharel Avatar answered Sep 18 '22 10:09

oriharel