Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getActivity() return 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 looks like this:

<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>

The problem is now shen I want to use a OnSharedPreferenceChangeListener in order to update the summary of some of my preferences.

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 am facing is that getActivity() is null when I have multiple categories in my xml header! The first one I open is always correct and non null, but when I press back, I come back to the Preference list automatically generated, I click on a second one and there, activity is null!

like image 854
Waza_Be Avatar asked Jan 17 '23 18:01

Waza_Be


1 Answers

OUCH! That was an hard one. I succeed to fix that.

In fact, the Listener was always belonging to the first Fragment. So when you change the Preference from an other category, the listener of the first Fragment is called when you change a Preference of the second Fragment!

So the Activity is null.

The solution is to remove the listener when you leave a Fragment, so the correct Listener can do his job:

@Override
public void onPause() {
    super.onPause();
    Log.i("", "PAUSE");
    preferences.unregisterOnSharedPreferenceChangeListener(this);
}
like image 166
Waza_Be Avatar answered Jan 31 '23 11:01

Waza_Be