FragmentPagerAdapter is deprecated problem

217 Views Asked by At

Android Studio says fragment pager adapter is deprecated. How can I remove this error? here is the screenshot

public static class ViewPagerAdapter extends FragmentPagerAdapter {
    private ArrayList<Fragment> fragments;
    private ArrayList<String> titles;
    
    public ViewPagerAdapter(@NonNull FragmentManager fm) {
        super(fm);
    }
1

There are 1 best solutions below

3
Md. Kabir Hassan On

The FragmentPagerAdapter class in Android is indeed deprecated as of AndroidX library version 1.2.0. It is recommended to use FragmentStatePagerAdapter or FragmentPagerAdapter2 instead.

  1. Replace FragmentPagerAdapter with FragmentStatePagerAdapter:

    public static class ViewPagerAdapter extends FragmentStatePagerAdapter {

  2. Update the constructor of ViewPagerAdapter to call the appropriate superclass constructor:

    public ViewPagerAdapter(@NonNull FragmentManager fm) { super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT); }

These changes will ensure that you are using the recommended replacement for FragmentPagerAdapter and resolve the deprecation problem.