FragmentStatePagerAdapter
is deprecated from API 27. What's the alternative of FragmentStatePagerAdapter
?
private class MainPagerAdapter extends FragmentStatePagerAdapter { MainPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { Fragment result = new DummyFragment(); return result; } @Override public int getCount() { return 5; } }
Above code shows FragmentStatePagerAdapter
, getItem(...)
and super(...)
as deprecated.
FragmentPagerAdapter is used exactly like FragmentStatePagerAdapter . It only differs in how it unloads your fragments when they are no longer needed (Figure 11.4). With FragmentStatePagerAdapter , your unneeded fragment is destroyed.
FragmentStateAdapter instead. Implementation of PagerAdapter that represents each page as a Fragment that is persistently kept in the fragment manager as long as the user can return to the page.
You can use startActivityForResult(or broadcast/receiver) to get the result from the activity. Then use adapter. notifyDataSetChanged to refresh the fragment.
FragmentStatePagerAdapter(fm,BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT). BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT, it means offset fragments will be executed on onStart() and the current fragment will be executed onStart() and onResume().
Switch to ViewPager2 and use FragmentStateAdapter instead.
From there you can use onPause and onResume callbacks to determine which fragment is currently visible for the user. onResume is called when a fragment became visible and onPause when it stops to be visible. -> read more
Based on the comments of Eric and Reejesh.
Old answer (deprecated too now)
The following constructors do the same
super(@NonNull FragmentManager fm) super(@NonNull FragmentManager fm, BEHAVIOR_SET_USER_VISIBLE_HINT)
Passing BEHAVIOR_SET_USER_VISIBLE_HINT
got deprecated. You should pass BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
instead.
The difference in passing those is explained in FragmentPagerAdapter
:
/** * Indicates that Fragment#setUserVisibleHint(boolean) will be * called when the current fragment changes. */ @Deprecated public static final int BEHAVIOR_SET_USER_VISIBLE_HINT = 0; /** * Indicates that only the current fragment will be * in the Lifecycle.State#RESUMED state. All other Fragments * are capped at Lifecycle.State#STARTED. */ public static final int BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT = 1;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With