1

In my app once user provide three inputs, AsyncTask starts which connects to the server and gets info for two more fields. Once AsyncTask is finished, I want to set those two received values in opened setting page.

I searched and try this code, but not updating summary for fields for which value has fetched from server.

Problem is I'm not able to set values received from server as summary to EditTextPreference. If I reopen settngs page it shows value not without reopening.

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    updatePreference(key);
}

private void updatePreference(String key) {
    if (key.equals("id")) {
        Preference preference = findPreference(key);
        if (preference instanceof EditTextPreference) {
            EditTextPreference editTextPreference = (EditTextPreference) preference;
            if (editTextPreference.getText().trim().length() > 0) {
                editTextPreference.setSummary(editTextPreference.getText());   
            } else {
                editTextPreference.setSummary("");
            }
        }

    } else if (key.equals("sclass")) {
        Preference preference = findPreference(key);
        if (preference instanceof EditTextPreference) {
            EditTextPreference editTextPreference = (EditTextPreference) preference;
            if (editTextPreference.getText().trim().length() > 0) {
                editTextPreference.setSummary(editTextPreference.getText());
                                    } else {
                editTextPreference.setSummary("");
            }
        }
    }
}
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
Panache
  • 1,701
  • 3
  • 19
  • 33

1 Answers1

0

Create a listener and call it in your onCreate

sharedPrefYourObj.registerOnSharedPreferenceChangeListener(t‌​his);

try to register you listener onResume and unregister it onPause though rather than onCreate!

example :

  protected void onResume() {
        super.onResume();
        getPreferenceScreen().getSharedPreferences()
                .registerOnSharedPreferenceChangeListener(this);
    }

    protected void onPause() {
        super.onPause();
        getPreferenceScreen().getSharedPreferences()
                .unregisterOnSharedPreferenceChangeListener(this);
    }
Charuක
  • 12,953
  • 5
  • 50
  • 88
  • I applied listener as per your guidance, It was already on resume but I added for pause as u suggested to unregister. Solution did not work. – Panache Jan 07 '17 at 13:34
  • @Panache do you have a listener in your onCreate – Charuක Jan 07 '17 at 13:34
  • public class FinalSettings extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener { – Panache Jan 07 '17 at 13:35
  • do I still need to add it oncreate? – Panache Jan 07 '17 at 13:35
  • @Panache you only implements the methods in that way.. you need to listen changes ..add a listener like this in your oncreate and see `SharedPreferences sharedPref = getPreferenceScreen().getSharedPreferences(); sharedPref.registerOnSharedPreferenceChangeListener(this);` – Charuක Jan 07 '17 at 13:37
  • Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.preference.PreferenceScreen.getSharedPreferences()' on a null object reference. Got this error after adding your line in oncreate. – Panache Jan 07 '17 at 13:46