Android ViewPager ViewPager with PreferenceFragment

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!


Until recently, using would prevent the usage of a PreferenceFragment as one of the Fragments used in the FragmentPagerAdapter.

The latest versions of the support v7 library now include the PreferenceFragmentCompat class, which will work with a ViewPager and the v4 version of FragmentPagerAdapter.

Example Fragment that extends PreferenceFragmentCompat:

import android.os.Bundle; 
import android.view.View; 
public class MySettingsPrefFragment extends PreferenceFragmentCompat { 
    public MySettingsPrefFragment() { 
        // Required empty public constructor 
    public void onCreate(Bundle savedInstanceState) { 
    public void onCreatePreferences(Bundle bundle, String s) { 

You can now use this Fragment in a subclass:

private class PagerAdapterWithSettings extends FragmentPagerAdapter {

    public PagerAdapterWithSettings(FragmentManager supportFragmentManager) {

    public Fragment getItem(int position) {
        switch(position) {
            case 0:
                return new FragmentOne();

            case 1:
                return new FragmentTwo();

            case 2:
                return new MySettingsPrefFragment();

                return null;
    // .......


Got any Android Question?