Replacement for BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT

666 views Asked by At

androidx.fragment.app.FragmentStatePagerAdapter is deprecated and developers are urged to switch to androidx.viewpager2.adapter.FragmentStateAdapter

How can we get the same functionality with FragmentStateAdapter that BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT provided?

This code below is now deprecated

FragmentStatePagerAdapter(
        fragmentManager,
        BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
    )
2

There are 2 answers

0
ianhanniballake On BEST ANSWER

The ViewPager2's FragmentStateAdapter has the same functionality as BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT (this is, in fact, the only type of behavior ViewPager2 supports).

The ViewPager2 migration guide details the exact steps needed to convert to ViewPager2.

0
navylover On

The answer is BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT

AndroidX introduces the new FragmentPagerAdapter constructor, please notice its second parameter int behavior.

And in fact, the BEHAVIOR_SET_USER_VISIBLE_HINT is deprecated, so BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT is the only recommended style.