Waza_Be
Waza_Be

Reputation: 39538

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!

Upvotes: 4

Views: 4745

Answers (2)

Waza_Be
Waza_Be

Reputation: 39538

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);
}

Upvotes: 10

Rajdeep Dua
Rajdeep Dua

Reputation: 11230

If your Listener is in a Fragment MyFragment use the following code to get the activity instance. We need to use .this.getActivity()

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

Upvotes: -1

Related Questions