ViewPager2 fragments lifecycle not working. Viewpager2 inside fragment - android

I have this structure
Tabs with fragments
---(one)Fragment with ViewPager2
------- Fragments inside ViewPager2
When I change between ViewPager's fragments, onResume and onPause are called just fine.
Problem comes when I click on another tab to load another fragment. When I do that for some reason the fragment that hosts the ViewPager will receive onHiddenChanged callback but its children will not receive any callback.
I've tried all 3 constructors for FragmentStateAdapter, none of them helped and I have no idea how I will notify viewpager's fragments for such a lifecycle change. Any ideas?

Related

call next fragment's lifecycle method when use viewpager2 with TabLayoutMediator

I use Viewpager2 With setup tablayout using TabLayoutMediator but the issue is
when I swipe viewpager first fragment to the second fragment here only call
current fragment's lifecycle methods, but when I click on tablayout
then call next fragment's lifecycle methods. I don't know why this happens?
This happens because the method that TabLayoutMediator uses to move to the selected fragment causes the RecyclerView to cache the next Fragment as it uses Smooth Scrolling.
This should not be a problem UNLESS you are doing something in the wrong part of the Fragment's lifecycle. If you only want do something when a Fragment is displayed then only do it in the Fragments onResume method as the behaviour you describe will only take the cached Fragment to "Started" state.

onPause(), onStop(), setUserVisibleHint(), and onHiddenChange() is not getting Called while using ViewPager Fragments

I have a ViewPager which contain 2 tabs(2 Separate fragments).
My Second Tab consist of further 6 child tabs (each separate fragment) in another ViewPager.
Now When i switch between 6 child tabs, setUserVisibleHint() method is called.
But when i am present on my any of 6 child tabs(i.e fragment) and i try to switch between the 2 parent tabs. None of the methods like onPause(), onStop(), setUserVisibleHint(), and onHiddenChange() of child tab fragment do not get called.
So can you please help me to know which method will get called of child fragment when i switch between 2 parent tabs?
And if no methods will get called of child fragments, then how can i achieve or know that there is switch between parent tabs and call a method in child fragments?
You shouldn't rely on a fragment lifecycle in this case. It's controlled by adapter and if any of those callbacks gets called it doesn't guarantee you anything.
Use onPageChangeListener instead. You can set it on a ViewPager object via addOnPageChangeListener in onResume for example, and call removeOnPageChangeListener in onPause. It has a method onPageChanged which will be called every time you switch the page.

Lifecycle of Fragment that holds a ViewPager

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?
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().

Android fragments fun with ViewPager

I'm using a ViewPager in my app. There are three fragments inside the PageAdapter that is set in the ViewPager. All works perfectly fine and I can switch between all three fragments. I have 3 buttons that allow me to switch between adapters 1 to 3. The problem is noticed when I click buttons 1 and 3 at the same time. Here's what happens:
Fragment 1 - Fragment 3
onAttach
onViewCreated
onResume
onPause
onAttach
onViewCreated
onResume
onPause
As you can see from above, the second fragment is never paused and therefore never resumed. Both fragments modify the same recycler view onResume so with the behaviour above I end up with the incorrect state of my recycler view.
Any thoughts on why this might be happening and how I can fix this?
A ViewPager loads (by default) the current page, the previous page and the next page, since you have only 3 pages the second fragment is always resumed.
the solution is to set up a OnPageChangeListener and modify the recycler view in onPageSelected instead in onResume

FragmentStatePagerAdapter doesn't call its fragments

I have FragmentStatePagerAdapter in a fragment with pager. Inside pager I have two fragments to switch between them. What is happening is that when I first come to fragment everything is done correctly, but when I go back and return there again then on the base fragment is called normally onCreate, onCreateView and so on, but on fragments in pager is not called anything, getItem on adapter is not called and pager view is blank. On all fragments I have setRetainInstance(true) which is needed due to a rotation behaviour handling.
Can you help me guys what to do with that?

Categories

Resources