0

I have a ViewPager which is used to display images or videos. So it uses two fragments. One fragment has ImageView and the other one has VideoView.

So when a user is watching a movie and swipes to another fragment I want the video to stop playing in the now offscreen fragment.

How can I do this?

tomi
  • 515
  • 8
  • 20

1 Answers1

1

Implement a ViewPager.OnPageChangeListener, it will give you the way to take action when the user goes from one page to another.

You set it by using setOnPageChangeListener

Matthieu
  • 15,277
  • 10
  • 54
  • 83
  • I knew about the OnPageChangeListener. The problem was how to get the current fragment so I could change it's properties. Found the answer here: http://stackoverflow.com/a/8886019/1143354 – tomi Nov 23 '12 at 08:55
  • I see... that part would depend on the PagerAdapter you are using (and how you implement it). Most of the time, I keep references to the different fragments I have separately (an Array for example), so that's usually not a problem. – Matthieu Nov 23 '12 at 09:07
  • I am using 'FragmentStatePagerAdapter' because there can be a lot of video or image files and this adapter cleans its views. So that is why I don't know if it's OK to keep references to fragments. – tomi Nov 23 '12 at 10:06
  • (I think) it's always ok to keep references to the fragments, only when you use them, you should check that getView() or getActivity() does not return null whenever you use them... the reference to the fragment is not going to change. – Matthieu Nov 24 '12 at 01:50