Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add PreferenceFragment on FragmentPagerAdapter

I use the last SDK to create an app with navigation type Tabs + Swipe and I want to put a PreferenceFragment on one of the tabs.

The generated code is like :

mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);

Meanwhile, I can't add PreferenceFragment on the SectionsPagerAdapter because its method getItem(int position) returns a Fragment, not a FragmentActivity.

I may need some help right here.

like image 317
BapNesS Avatar asked Jan 27 '26 18:01

BapNesS


2 Answers

The issue is that ViewPager's getItem(..) method returns a support Fragment (i.e. android.support.v4.app.Fragment) rather than android.app.Fragment, which is what PreferenceFragments extend. Basically, a PreferenceFragment uses the non-support version of the APIs and so is incompatible with ViewPagers, and there is no support version of PreferenceFragment available.

like image 82
Alex Curran Avatar answered Jan 30 '26 07:01

Alex Curran


You can upgrade to a v13 pager which supports the android.app.Fragment. I gave a detailed answer on a similar question here.

like image 28
PseudoNoise Avatar answered Jan 30 '26 07:01

PseudoNoise



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!