Problem: Fragment onResume()
in ViewPager
is fired before the fragment becomes actually visible.
For example, I have 2 fragments with ViewPager
and FragmentPagerAdapter
. The second fragment is only available for authorized users and I need to ask the user to log in when the fragment becomes visible (using an alert dialog).
BUT the ViewPager
creates the second fragment when the first is visible in order to cache the second fragment and makes it visible when the user starts swiping.
So the onResume()
event is fired in the second fragment long before it becomes visible. That's why I'm trying to find an event which fires when the second fragment becomes visible to show a dialog at the appropriate moment.
How can this be done?
We have a special case with MVP where the fragment needs to notify the presenter that the view has become visible, and the presenter is injected by Dagger in
fragment.onAttach()
.setUserVisibleHint()
is not enough, we've detected 3 different cases that needed to be addressed (onAttach()
is mentioned so that you know when the presenter is available):Fragment has just been created. The system makes the following calls:
Fragment already created and home button is pressed. When restoring the app to foreground, this is called:
Orientation change:
We only want the visibility hint to get to the presenter once, so this is how we do it:
Detecting by
focused view
!This works for me
I encountered this problem when I was trying to get a timer to fire when the fragment in the viewpager was on-screen for the user to see.
The timer always started just before the fragment was seen by the user. This is because the
onResume()
method in the fragment is called before we can see the fragment.My solution was to do a check in the
onResume()
method. I wanted to call a certain method 'foo()' when fragment 8 was the view pagers current fragment.Hope this helps. I've seen this problem pop up a lot. This seems to be the simplest solution I've seen. A lot of others are not compatible with lower APIs etc.
UPDATE: Android Support Library (rev 11) finally fixed the user visible hint issue, now if you use support library for fragments, then you can safely use
getUserVisibleHint()
or overridesetUserVisibleHint()
to capture the changes as described by gorn's answer.UPDATE 1 Here is one small problem with
getUserVisibleHint()
. This value is by defaulttrue
.So there might be a problem when you try to use it before
setUserVisibleHint()
was invoked. As a workaround you might set value inonCreate
method like this.The outdated answer:
In most use cases,
ViewPager
only show one page at a time, but the pre-cached fragments are also put to "visible" state (actually invisible) if you are usingFragmentStatePagerAdapter
inAndroid Support Library pre-r11
.I override :
To capture the focus state of fragment, which I think is the most suitable state of the "visibility" you mean, since only one fragment in ViewPager can actually place its menu items together with parent activity's items.
Try this, it's work for me:
I had the same issue.
ViewPager
executes other fragment life cycle events and I could not change that behavior. I wrote a simple pager using fragments and available animations. SimplePager