Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7165497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:15:34+00:00 2026-05-28T14:15:34+00:00

In my application, I used PreferenceFragment to create a nice application on Tablets and

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:15:35+00:00Added an answer on May 28, 2026 at 2:15 pm

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Our application used to make use of a common base form that all forms
My application used to use an MFC CEdit control and we had no problems
I create one application used form class now its very simple,I want set background
I want to use an Demo application used in Cocos2D 0.9.3 to my demo
I have a list view in an Android application used to display an ArrayList
In my MFC application I used CSplitterWnd to create two panes and each pane
I have an application used by pretty tech-savey people and they want small island
I develop a critical application used by a multi-national company. Users in offices all
I am currently building an internal web application used in a factory/warehouse type location.
I am developing a scientific application used to perform physical simulations. The algorithms used

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.