使用ViewPager2和FragmentStateAdapter的片段生命周期行为

问题描述 投票:2回答:1

我正在使用fragmentsViewPager2显示5 FragmentStateAdapter。每个fragment具有ExoPlayer实例。我正在维护ViewPager2.setOffscreenPageLimit(1),并且可以很好地加载fragments。但是,当我向下滚动ViewPager2pos4时,pos2pos1应该调用onStop()onDestroyView()onDestroy()。但这没有发生。

除非我按onBackPress(),否则这些方法均未执行。 FragmentStateAdapter的实现部分是否缺少任何内容。

我想在exoPlayer.release()上执行onDestroyView()以释放应用程序的内存。

这里是代码

活动

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();
    }

}

更新

This is also a similar question

android android-viewpager fragment exoplayer android-viewpager2
1个回答
0
投票

我已经介绍过此POST用于解决方案。

[VeiwPager2建立在RecyclerView组件上,并且FragmentStateAdapter代替FragmentStatePagerAdapter

假设offscreenPageLimit = 1ViewPager2将具有3 fragments2 fragments预先加载/缓存的RecylerView Preloading Strategies,因此,总共有5 fragments已加载。

要禁用预加载和缓存,我执行了:

private void disableCacheOfViewPager() {

    try {

        // Disable Preloading
        ((RecyclerView) binding.pager.getChildAt(0)).getLayoutManager().setItemPrefetchEnabled(false);
        // Disable Cache
        ((RecyclerView) binding.pager.getChildAt(0)).setItemViewCacheSize(0);

    } catch (NullPointerException e) {

        Log.i(TAG, "disableCacheOfViewPager: " + e.getMessage());
    }
}

之前滚动日志

//1st
2020-03-02 18:05:15.255 12541-12541/com.example.rajat.demo I/PostFragment: onStart: 0
2020-03-02 18:05:15.256 12541-12541/com.example.rajat.demo I/PostFragment: onResume: 0
2020-03-02 18:05:15.375 12541-12541/com.example.rajat.demo I/PostFragment: onStart: 1

//2nd
2020-03-02 18:09:16.746 12541-12541/com.example.rajat.demo I/PostFragment: onStart: 2
2020-03-02 18:09:16.900 12541-12541/com.example.rajat.demo I/PostFragment: onPause: 0
2020-03-02 18:09:16.904 12541-12541/com.example.rajat.demo I/PostFragment: onResume: 1

//3rd
2020-03-02 18:09:49.759 12541-12541/com.example.rajat.demo I/PostFragment: onStart: 3
2020-03-02 18:09:49.940 12541-12541/com.example.rajat.demo I/PostFragment: onPause: 1
2020-03-02 18:09:49.945 12541-12541/com.example.rajat.demo I/PostFragment: onResume: 2

//4th
2020-03-02 18:10:06.636 12541-12541/com.example.rajat.demo I/PostFragment: onStart: 4
2020-03-02 18:10:06.826 12541-12541/com.example.rajat.demo I/PostFragment: onPause: 2
2020-03-02 18:10:06.830 12541-12541/com.example.rajat.demo I/PostFragment: onResume: 3

//5th
2020-03-02 18:10:17.737 12541-12541/com.example.rajat.demo I/PostFragment: onStart: 5
2020-03-02 18:10:18.070 12541-12541/com.example.rajat.demo I/PostFragment: onStop: 0
2020-03-02 18:10:18.071 12541-12541/com.example.rajat.demo I/PostFragment: onDestroyView: 0
2020-03-02 18:10:18.139 12541-12541/com.example.rajat.demo I/PostFragment: onDestroy: 0
2020-03-02 18:10:18.139 12541-12541/com.example.rajat.demo I/PostFragment: onDetach: 0
2020-03-02 18:10:18.144 12541-12541/com.example.rajat.demo I/PostFragment: onPause: 3
2020-03-02 18:10:18.148 12541-12541/com.example.rajat.demo I/PostFragment: onResume: 4

之后滚动日志

//1st
2020-03-02 18:11:20.439 14463-14463/com.example.rajat.demo I/PostFragment: onStart: 0
2020-03-02 18:11:20.441 14463-14463/com.example.rajat.demo I/PostFragment: onResume: 0
2020-03-02 18:11:20.642 14463-14463/com.example.rajat.demo I/PostFragment: onStart: 1

//2nd
2020-03-02 18:11:43.833 14463-14463/com.example.rajat.demo I/PostFragment: onStart: 2
2020-03-02 18:11:43.948 14463-14463/com.example.rajat.demo I/PostFragment: onPause: 0
2020-03-02 18:11:43.956 14463-14463/com.example.rajat.demo I/PostFragment: onResume: 1

//3rd
2020-03-02 18:11:52.523 14463-14463/com.example.rajat.demo I/PostFragment: onStart: 3
2020-03-02 18:11:52.686 14463-14463/com.example.rajat.demo I/PostFragment: onStop: 0
2020-03-02 18:11:52.686 14463-14463/com.example.rajat.demo I/PostFragment: onDestroyView: 0
2020-03-02 18:11:52.753 14463-14463/com.example.rajat.demo I/PostFragment: onDestroy: 0
2020-03-02 18:11:52.753 14463-14463/com.example.rajat.demo I/PostFragment: onDetach: 0
2020-03-02 18:11:52.758 14463-14463/com.example.rajat.demo I/PostFragment: onPause: 1
2020-03-02 18:11:52.761 14463-14463/com.example.rajat.demo I/PostFragment: onResume: 2
© www.soinside.com 2019 - 2024. All rights reserved.