Skip to content Skip to sidebar Skip to footer

How To Stop Videoview From Playing Inside A Viewpager When Fragment Is Changed

I want to stop a videoView from playing inside a viewPager when the fragment is change. The problem in the code is when I change or get into the next fragment the video is still pl

Solution 1:

In your fragment override override fun setUserVisibleHint(isVisibleToUser: Boolean)

because setUserVisibleHint is called before onCreateView Make sure you check if the view is not null (so that you can avoid null pointer exceptions).

Here's how I implemented mine:

overridefunsetUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (view != null)
            if (!isVisibleToUser)
                if (videoView.isPlaying) {
                    videoView.stopPlayback()
                }
    }

and I am extending the Fragment androidx.fragment.app.Fragment

Post a Comment for "How To Stop Videoview From Playing Inside A Viewpager When Fragment Is Changed"