I am displaying 5 fragments
using ViewPager2
and FragmentStateAdapter
. Each fragment
has ExoPlayer
instance. I am maintaining ViewPager2.setOffscreenPageLimit(1)
and it is working well for loading fragments
. However, when I scroll down the ViewPager2
to pos4
, pos2
and pos1
are supposed to call onStop()
, onDestroyView()
, onDestroy()
. But that is not happening.
None of these methods are executing unless I press onBackPress()
. Is there anything I am missing with the implementation part of FragmentStateAdapter
.
I want to perform exoPlayer.release()
on onDestroyView()
to free the memory of the application.
Here is the code
Activity
List<Post> postsList = response.getPosts();
ViewPagerAdapter adapter = new ViewPagerAdapter(this, postsList);
binding.pager.setOrientation(ViewPager2.ORIENTATION_VERTICAL);
binding.pager.setOffscreenPageLimit(1);
binding.pager.setAdapter(adapter);
ViewPagerAdapter
public class ViewPagerAdapter extends FragmentStateAdapter {
private List<Post> postList;
public ViewPagerAdapter(@NonNull FragmentActivity fragmentActivity, List<Post> postList) {
super(fragmentActivity);
this.postList = postList;
}
@NonNull
@Override
public Fragment createFragment(int position) {
return new PostFragment(postList.get(position), position);
}
@Override
public int getItemCount() {
if (postList == null) return 0;
return postList.size();
}
}
Updated