In my application, I used PreferenceFragment to create a nice application on tablets and smartphones.
So, in my main activity I use:
@Override public void onBuildHeaders(List<Header> target) { loadHeadersFromResource(R.xml.preference_headers, target); }
My xml file is as follows:
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android"> <header android:fragment="tof.cv.mpp.view.StockPreferenceFragment" android:title="@string/btn_home_settings" android:summary=""> <extra android:name="resource" android:value="activity_preferences" /> </header> <header android:fragment="tof.cv.mpp.view.StockPreferenceFragment" android:title="@string/btn_home_planner" android:summary=""> <extra android:name="resource" android:value="activity_planner_preferences" /> </header> <header android:fragment="tof.cv.mpp.view.StockPreferenceFragment" android:title="@string/btn_home_twitter" android:summary=""> <extra android:name="resource" android:value="activity_twitter_preferences" /> </header> </preference-headers>
Now the problem is shen. I want to use OnSharedPreferenceChangeListener to update the summary of some of my settings.
I use:
@Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { Log.i("", "PREFChanged "+getActivity()); if (key.contentEquals("prefPseudo")) { Log.i("", "PseudoChanged"); Preference pref = findPreference("prefPseudo"); pref.setSummary(((EditTextPreference) pref).getText()); } if (key.contentEquals(getString(R.string.key_activity))) { Log.i("", "FirstChanged"); Preference pref = findPreference(getString(R.string.key_activity)); pref.setSummary(((ListPreference) pref).getEntry()); } if (key.contentEquals(getString(R.string.key_planner_da))) { Preference pref = findPreference(getString(R.string.key_planner_da)); Log.i("", "PlannerChanged"+pref); pref.setSummary(((ListPreference) pref).getEntry()); } }
The big problem I ran into is that getActivity () is null when I have multiple categories in my xml header! The first one I open is always correct and not null, but when I click, I return to the settings list automatically generated, I click on the second, and there the activity is zero!
source share