I have a ViewPager
with multiple fragments. In one Fragment
I play audio. When I swipe to another fragment I want to stop the audio playback. How do I detect that the another fragment is now visible in the ViewPager
?
I've tried overriding onStop
and onHiddenChanged
. No success. There must be some "you're not active anymore" method to override. No?
I did some digging and it turns out that ViewPager will call both:
setUserVisibleHint
andsetMenuVisibility
. I would overridesetUserVisibleHint
since the documentation for setUserVisibleHint states:Try putting this code in your fragment: