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);
}
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.
Replace FragmentPagerAdapter with FragmentStatePagerAdapter:
public static class ViewPagerAdapter extends FragmentStatePagerAdapter {
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.