raghu_3
raghu_3

Reputation: 1899

Preferences not getting updated until the app is restarted

I am taking IP ,Port and Device Name settings from User and using them in initializing an object. However when i am putting settings ,those settings are not getting updated in app ,until i restart the app.

Here is my Settings File-

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        addPreferencesFromResource(R.layout.settings);

        SharedPreferences sp = getPreferenceScreen().getSharedPreferences();
        EditTextPreference editTextPrefLAN = (EditTextPreference) findPreference("prefABCLan");
        editTextPrefLAN
                .setSummary(sp.getString("prefABCLan", "Set your LAN IP"));





        EditTextPreference editTextPrefPort = (EditTextPreference) findPreference("prefABCPort");
        editTextPrefPort
                .setSummary(sp.getString("prefABCPort", "Set your port"));
        EditTextPreference editTextPrefDeviceName = (EditTextPreference) findPreference("prefABCDeviceName");
        editTextPrefDeviceName
                .setSummary(sp.getString("prefABCDeviceName", "Set your device name"));

        /*EditTextPreference editTextPrefScopeIP = (EditTextPreference) findPreference("prefScopeIP");
        editTextPrefScopeIP
                .setSummary(sp.getString("prefScopeIP", "Set your Scope IP"));
        EditTextPreference editTextPrefScopeLogin = (EditTextPreference) findPreference("prefScopeLogin");

*/


    }

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

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

    protected void onPause() {
        super.onPause();
        getPreferenceScreen().getSharedPreferences()
                .unregisterOnSharedPreferenceChangeListener(this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.actionbar, menu);

        return true;
    }

    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
                                          String key) {
        Preference pref = findPreference(key);
        if (pref instanceof EditTextPreference) {
            EditTextPreference etp = (EditTextPreference) pref;
            pref.setSummary(etp.getText());
        }
    }

Is there anything i need to do in my code?

Upvotes: 1

Views: 296

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006584

When the user modifies preferences in a PreferenceActivity, you still need to do something elsewhere in your app to use those preferences. This is particularly true if you read the preferences earlier, and now need the changed values.

One approach is just to reload the preferences, by putting your preference-reading logic in a lifecycle method like onResume() that will be called both when the activity/fragment is created and when it returns to the screen after the user visited the PreferenceActivity.

Another approach is to use the OnSharedPreferenceChangeListener in the other components, just as you are using it here. Activities (or fragments or whatever) that care about preference changes would register an OnSharedPreferenceChangeListener and watch for relevant changes.

Upvotes: 1

Related Questions