Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Recently the androidx.fragment.app.FragmentManager is deprecated and no proper solutions are available.

Have tried to implement support V4 but did not work with AndroidX . It shows library not found.

PagerAdapter:

public ViewPagerAdapter(FragmentManager manager) {
    super(manager);
    //...

Thanks in advance.

Both FragmentStatePagerAdapter and FragmentPagerAdapter have been deprecated. We need to use ViewPager2 + FragmentStateAdapter since AndroidX. Follow this: stackoverflow.com/a/66545099/8664401 – aQwus jargon Mar 9, 2021 at 10:31

It is not deprecated at the present time. For example, it is not marked as deprecated in the documentation.

'FragmentStatePagerAdapter(androidx.fragment.app.FragmentManager)' is deprecated

The single-parameter FragmentStatePagerAdapter constructor is deprecated. However, if you read the documentation for that constructor, you will find:

This constructor is deprecated. use FragmentStatePagerAdapter(FragmentManager, int) with BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT

So, replace FragmentStatePagerAdapter(fm) with FragmentStatePagerAdapter(fm, FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT), to retain the functionality from the original one-parameter constructor.

Thanks, can try super(fm,FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT); – Latief Anwar Jul 13, 2019 at 9:23 today replace this: super(fm,FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT); to this: super(fm,FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT); the answer is also deprecated now – dherediat Jul 3, 2020 at 11:06

You can change default constructor as below:

public SectionsPagerAdapter(@NonNull FragmentManager fm, int behavior, Context mContext) {
    super(fm, behavior);
    this.mContext = mContext;

Full Adapter class as defined:

* A [FragmentPagerAdapter] that returns a fragment corresponding to * one of the sections/tabs/pages. public class SectionsPagerAdapter extends FragmentStatePagerAdapter { @StringRes private static final int[] TAB_TITLES = new int[]{R.string.tab_text_1, R.string.tab_text_2}; private final Context mContext; public SectionsPagerAdapter(@NonNull FragmentManager fm, int behavior, Context mContext) { super(fm, behavior); this.mContext = mContext; @NotNull @Override public Fragment getItem(int position) { // getItem is called to instantiate the fragment for the given page. // Return a ProductSearchFragment (defined as a static inner class below). if(position == 0) { return new ProductSearchFragment(); } else if(position == 1) { return new GenericSearchFragment(); return new ProductSearchFragment(); @Nullable @Override public CharSequence getPageTitle(int position) { return mContext.getResources().getString(TAB_TITLES[position]); @Override public int getCount() { // Show 2 total pages. return 2;

and You can call like:

SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT, this);

Thank you.

public SectionsPagerAdapter(@NonNull FragmentManager fm,
                            int behavior) {
    super(fm,behavior);

Alternatively you can go

Right Click -> Generate -> Override Methods and click on the first item as shown in the image.enter image description here

You may need to change your code in other places after doing this.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.