1

I have something that has me stumped. I have a Fragment (Fragment A) that has ViewPager that contains three fragments (for swiping left/right). So, if within another fragment, in the onBackPressed() method, I do a getFragmentManager().popBackStack() call, Fragment A will be again be visible (with the ViewPager of sub-fragments) which is the desired state. However, there is no method with Fragment A or within the ViewPager that indicates that Fragment A/ViewPager is again visible.

None of the fragment methods referenced in the Fragment lifecycle (http://developer.android.com/guide/components/fragments.html) that should be called when "The fragment returns the layout from the back stack" or any of the methods called within OnPageChangeListener (yes, I do call viewPager.setOnPageChangeListener(this) within Fragment A's onCreateView).

Thoughts on where I could look?

Perry Hoekstra
  • 2,687
  • 3
  • 33
  • 52
  • Is this the situation you are describing? Frag-A and its sub-frags are visible. Some event causes them to be removed/replaced by Frag-B and put into the back-stack, making them not visible. User presses Back, causing Frag-A and its sub-frags to be restored to the layout from the back-stack, making them visible again. That all works, but you don't know how to determine that Frag-A and its sub-frags are visible. The lifecycle diagram in the Frag Guide you linked shows the methods called when a fragment goes into, or comes out of the back-stack. Can't you derive the visible state from those? – Bob Snyder Jul 02 '15 at 00:30
  • Yes, you are correct on all points. The lifecycle as defined in the Android docs is not followed. None of those methods are called when Frag-A and the contained ViewPager of sub-fragments is made visible. – Perry Hoekstra Jul 02 '15 at 17:17

1 Answers1

0

I have an app with a ViewPager that is added to the layout, then later replaced by another fragment, with the change added to the transaction back stack. I have Log statements in each of the lifecycle methods of the pager. When the Back button is pressed and the pager is returned to the layout, my logcat output shows these methods called for the pager: onCreateView(), onActivityCreated(), onStart(), onResume(). Note that when a fragment goes into the back stack, its view is destroyed, but the fragment object is not destroyed, so when the fragment returns from the back stack, there is no call to onCreate().

This behavior is consistent with the lifecycle diagram in the Fragment Guide. You should be able to use the call to onResume() as an indication that your pager is visible. I can only suggest that you add debug output to the lifecycle methods for your pager and look at the output. If you think its wrong, please add it to the post of your question. Also indicate what fragment transaction method(s) you are using. If by chance you are using hide() instead of remove() or replace(), then the lifecycle events are different and you may need to use onHiddenChanged().

Bob Snyder
  • 37,759
  • 6
  • 111
  • 158
  • I am glad to hear it but in my case, those methods are NOT called. I also have both Log messages in those methods and I have also tried debug stops with no joy in either case. That is the reason for the StackOverflow question. – Perry Hoekstra Jul 02 '15 at 19:25
  • In regards to what fragment transaction methods I am using, it is Fragment fragment = (Fragment) clazz.newInstance(); FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.tab_fragment, fragment).addToBackStack(fragmentName); fragmentTransaction.commit(); – Perry Hoekstra Jul 02 '15 at 19:28
  • Sounds pretty bizarre. When you exit (finish) the activity that contains the fragment, do you see calls to the lifecycle methods for fragment teardown? Similarly, when the pager is first added to the layout, do you see creation lifecycle methods called? – Bob Snyder Jul 02 '15 at 20:00
  • I don't have log messages in the teardown methods as I really did not care about them. I do see the creation lifecycle methods called as Frag-A and its ViewPager is first created. – Perry Hoekstra Jul 02 '15 at 20:08
  • With your hierarchy of fragments (sub-frags are children of pager, pager is child of Frag-A), are you using getChildFragmentManager() of parent frag to add/replace children? I'm also wondering about your use of `onBackPress()`. That is in your activity, so you are popping the stack of the activity's fragment manager. Are there cases where you want to pop a child's fragment manager. – Bob Snyder Jul 02 '15 at 21:55
  • Also, if you actually structured your fragments as parent/children, make sure you call through to super on each lifecycle method. That is what propagates the call to the children. – Bob Snyder Jul 02 '15 at 22:25
  • In regards to super, yes, I do. – Perry Hoekstra Jul 02 '15 at 23:37