1

I cannot figure out how to get PreferenceFragment to work correctly. I have a ViewPager hooked up to a FragmentPagerAdapter, with two Fragments that the user can swipe between. I am trying to get the "Settings" menu working, using a PreferenceFragment, but I am unsure what I'm doing wrong. When I tap Settings the view is changed to a blank white screen.

My SettingsFragment class:

public class SettingsFragment extends PreferenceFragment{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }
}

My PagerAdapter class:

public class PagerAdapter extends FragmentPagerAdapter {

    SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();

    public PagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position){
        Fragment fragment = (Fragment) super.instantiateItem(container, position);
        registeredFragments.put(position, fragment);
        return fragment;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        registeredFragments.remove(position);
        super.destroyItem(container, position, object);
    }

    @Override
    public Fragment getItem(int i) {
        switch  (i) {
            // default case is also case 0 to avoid redundant code
            default: return new CalculatorFragment();
            case 1: return new TapeFragment();
        }
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 1: return "Review";
            default: return "Calculator";
        }
    }

    public Fragment getRegisteredFragment(int position){
        return registeredFragments.get(position);
    }

}

My main class which should create the PreferenceFragment when user taps "Settings"

public class MangoCalc extends FragmentActivity implements CalculatorFragment.CalcTapeInterface{

    PagerAdapter pagerAdapter;
    ViewPager myViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        final ActionBar actionBar = getActionBar();

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mango_calc);

        pagerAdapter = new PagerAdapter(getFragmentManager());
        myViewPager = (ViewPager) findViewById(R.id.main);
        myViewPager.setAdapter(pagerAdapter);

        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        ActionBar.TabListener tabListener = new ActionBar.TabListener() {
            @Override
            public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
                myViewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {}

            @Override
            public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {}
        };

        for (int i=0; i< pagerAdapter.getCount(); i++) {
            actionBar.addTab(actionBar.newTab()
                    .setText(pagerAdapter.getPageTitle(i))
                    .setTabListener(tabListener));
        }

        myViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener());
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.mango_calc, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            FragmentManager fm = getFragmentManager();
            SettingsFragment settingsFragment = new SettingsFragment();
            fm.beginTransaction().replace(R.id.main, settingsFragment).commit();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onTapePass(ArrayList<String> data) {
        if (data!=null) Log.d("Tape", "Last element got: "+data.get(data.size()-1));
        TapeFragment tapeFragment = (TapeFragment) pagerAdapter.getRegisteredFragment(1);
        tapeFragment.updateTape(data);
    }
}

My preferences.XML

<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
 <PreferenceCategory android:title="@string/lookandfeel">
     <ListPreference
         android:key="theme_preference"
         android:title="@string/theme"
         android:summary="@string/summary_theme_preference"
         android:entries="@array/entries_theme_preference"
         android:entryValues="@array/entryvalues_theme_preference"
         android:defaultValue="mango"/>
    <SwitchPreference
        android:key="keypress_vibration_preference"
        android:title="@string/keypress_vibration"
        android:summaryOn="@string/summaryon_keypress_vibration_preference"
        android:summaryOff="@string/summaryoff_keypress_vibration_preference"
        android:switchTextOn="@string/text_keypress_vibration_on"
        android:switchTextOff="@string/text_keypress_vibration_off"
        android:defaultValue="true" />
 </PreferenceCategory>
</PreferenceScreen>

and the XML for the activity:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main"
/>
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Eric
  • 673
  • 5
  • 18
  • Possible duplicate of [Adding PreferenceFragment to FragmentPagerAdapter](https://stackoverflow.com/questions/15845632/adding-preferencefragment-to-fragmentpageradapter) – A-Sharabiani Apr 08 '18 at 04:07

1 Answers1

0

In your getitem case 1 needs to be listed before the default

dabluck
  • 1,641
  • 2
  • 13
  • 20
  • While that did need to be fixed, that's not the cause of the problem. – Eric Aug 02 '14 at 20:37
  • Did you figure it out? If so post the answer for future people browsing. – dabluck Aug 04 '14 at 16:32
  • I ended up having the settings button start a new activity instead, and attaching the preferenceFragment to that. It's not the right way to do things but it works. Thus also forced me to use sharedPreferences to save the state of the fragments so that the user can keep working seamlessly after changing settings, which needed to be done anyways. – Eric Aug 04 '14 at 17:14