0

I'm tryng study the Preference system of Android.

When from my navdrawer I call settings fragment, it doesn't show nothing and menu remain selected, also if I select another voice.

This is the screen at startup of my APP:

enter image description here

Where all is perfect. After I click on Impostazioni (settings, in English), Home (or the "Elenca" item) reamin checked, also with "Impostazioni"

enter image description here

Another one problem is that Settings frame is blank (no error in Log but it's all blank).

This is XML preferences.xml stored under res/xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">


    <PreferenceCategory
        android:title="@string/pref_sms_storage_title"
        android:key="pref_key_storage_settings">
        <CheckBoxPreference
            android:key="pref_sync"
            android:title="@string/pref_sync"
            android:summary="@string/pref_sync_summ"
            android:defaultValue="true" />
    </PreferenceCategory>


</PreferenceScreen>

This is my Settings Fragment:

import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.support.v4.app.Fragment;

import com.xxxxxx.R;

public class SettingsFragment extends Fragment{

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        new InnerSettingsFragment();
    }

    public static class InnerSettingsFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.preferences);
        }
    }

}

And finally this is my BaseApp that mantains the navdrawer logic:

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

            // This method will trigger on item Click of navigation menu
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {


                //Checking if the item is in checked state or not, if not make it in checked state
                if (menuItem.isChecked()) menuItem.setChecked(false);
                else menuItem.setChecked(true);

                //Closing drawer on item click
                drawerLayout.closeDrawers();


                //Check to see which item was being clicked and perform appropriate action
                switch (menuItem.getItemId()) {




                    case R.id.home:

                        DashboardFragment dashboardFragment = new DashboardFragment();
                        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction.replace(R.id.frame, dashboardFragment,"DASHBOARD_FRAGMENT");
                        fragmentTransaction.commit();
                        return true;

                    case R.id.list_event:
                        ListEventFragment fragmentListEvent = new ListEventFragment();
                        fragmentTransaction = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction.replace(R.id.frame, fragmentListEvent);
                        fragmentTransaction.commit();
                        return true;

                    case R.id.settings:
                        SettingsFragment fragmentSettings = new SettingsFragment();
                        fragmentTransaction = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction.replace(R.id.frame, fragmentSettings);
                        fragmentTransaction.commit();
                        return true;






                    default:

                        return true;

                }
            }
        });
sineverba
  • 5,059
  • 7
  • 39
  • 84

1 Answers1

0

You shouldn't make your PreferenceFragment an inner class, add your outer class in its place, and still expect things to work as usual. It won't because the outer onCreate() wouldn't delegate to the inner onCreate() by itself. So, just make your InnerSettingsFragment a regular top-level class and you'll start seeing your preferences get loaded from the XML resource.

public class SettingsFragment extends PreferenceFragment {

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.preferences);
        }
}
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
  • Oh, thank you, it works, but I have the problem that double items are checked at same time... I'm going to edit my question. Thank you! – sineverba Jul 06 '15 at 19:42
  • Found what code is giving me the issue. I'll open new question, very different. Thank you very much! – sineverba Jul 06 '15 at 19:57
  • You're welcome. Do post a link here. I'll take a look in the morning in case nobody answers. – Ravi K Thapliyal Jul 06 '15 at 20:00
  • This is new question: http://stackoverflow.com/questions/31254626/android-issue-with-menu-item-that-are-multiple-checked Have a nice day ;-) – sineverba Jul 06 '15 at 20:03