GetActivity () returns null in PreferenceFragment

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!

+4
source share
2 answers

OH! This was hard. I managed to fix it.

In fact, the listener always belonged to the first fragment. Therefore, when you change the preference from another category, the listener of the first fragment is called when you change the preference of the second fragment!

So the action is null.

The solution is to remove the listener when you leave the fragment, so the right listener can do its job:

 @Override public void onPause() { super.onPause(); Log.i("", "PAUSE"); preferences.unregisterOnSharedPreferenceChangeListener(this); } 
+10
source

If your listener is in a MyFragment fragment, use the following code to get an activity instance. We need to use .this.getActivity ()

 @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { Log.i("", "PREFChanged "+ MyFragment.this.getActivity()); ..... } 
-1
source

Source: https://habr.com/ru/post/1391681/


All Articles