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

Recently the androidx.fragment.app.FragmentManager is deprecated

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.


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.


Replace

public SectionsPagerAdapter(FragmentManager fm) {

    super(fm);
}

With

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.