28

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.

zappee
  • 20,148
  • 14
  • 73
  • 129
Saswata
  • 1,290
  • 2
  • 14
  • 28
  • Both `FragmentStatePagerAdapter` and `FragmentPagerAdapter` have been deprecated. We need to use `ViewPager2 + FragmentStateAdapter` since **AndroidX**. Follow this: https://stackoverflow.com/a/66545099/8664401 – aQwus jargon Mar 09 '21 at 10:31

5 Answers5

47

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 2
    Thanks, can try super(fm,FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT); – Latief Anwar Jul 13 '19 at 09:23
  • 1
    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 03 '20 at 11:06
15

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.

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
3

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.

DragonFire
  • 3,722
  • 2
  • 38
  • 51
1

Below solution in Kotlin :

class TasksPagerAdapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm,BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT )
Alok Gupta
  • 1,806
  • 22
  • 21
-1

I ran into the same problem.

Change:

private class TestTest extends FragmentStatePagerAdapter

To:

private class TestTest extends FragmentPagerAdapter {

It will change:

import androidx.legacy.app.FragmentStatePagerAdapter;

To:

import androidx.fragment.app.FragmentPagerAdapter;
Marc Alexander
  • 761
  • 11
  • 24