Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment's onResume() not called when popped from backstack

Tags:

Hi I am developing android application in which I am using I am using single Activity and 3 fragments. So consider I have 3 fragments A B C. When I switch from A to B, I am adding Fragment to backstack and simillar for B to C. Now when I click back from C it shows me B and similar for B to A as well.

But thing is that when I come from C to B or B to A, it's not calling onResume() or any other life cycle methods of Fragment.

What I want to do actually for every Fragment I have different title in ActionBar. So, in my code, when I move from A to B or B to c, I am changing activity title inside fragment. But when I click on back it not changing according to that.

What is the actual problem? Why after pop from backstack its not calling onResume() for my Fragment? How do I solve this problem? Need Help. Thank you.

like image 510
nilkash Avatar asked Oct 16 '13 05:10

nilkash


People also ask

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 .

How do you write a fragment resume?

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.

What is the fragment life cycle in Android?

Fragment life cycle is closely related to the life cycle of its host activity which means when the activity is paused, all the fragments available in the activity will also be stopped. A fragment can implement a behaviour that has no user interface component.


1 Answers

onResume() of the Fragment is called only when the Activity is resumed. So this wont help you. Even I'm facing similar issue right now. You can implement OnBackStackChangedListener and get the fragment name in the top of the stack and set the ActionBar title based on that.

private FragmentManager.OnBackStackChangedListener getListener() {     FragmentManager.OnBackStackChangedListener result = new FragmentManager.OnBackStackChangedListener()     {         public void onBackStackChanged()         {             FragmentManager manager = getFragmentManager();              if (manager != null)             {                 if(manager.getBackStackEntryCount() >= 1){                     String topOnStack = manager.getBackStackEntryAt(manager.getBackStackEntryCount()-1).getName();                     Log.i("TOP ON BACK STACK",topOnStack);                 }                 }             }     };      return result; } 
like image 172
LoveMeSomeFood Avatar answered Sep 21 '22 08:09

LoveMeSomeFood