Reputation: 1348
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.
Upvotes: 27
Views: 19856
Reputation: 4102
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.
You may need to change your code in other places after doing this.
Upvotes: 3
Reputation: 1910
Below solution in Kotlin :
class TasksPagerAdapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm,BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT )
Upvotes: 1
Reputation: 831
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;
Upvotes: -1
Reputation: 62419
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.
Upvotes: 15
Reputation: 1007658
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.
Upvotes: 46