Keith Adler
Keith Adler

Reputation: 21178

Refresh preferences activity once I have cleared and committed changes

I have an Android PreferencesAcitivty that allows users to reset their Preferences to their default values. The code below works fine, but the Preferences interface does not update after I call editor.clear() and editor.commit(). How can I achieve this without reloading the activity or calling onCreate(null)?

public class Preferences extends PreferenceActivity {

    /* Called when someone specifies to view the options menu */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Set
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.settings, menu);

        // Return
        return true;
    }

    /* Handle options menu selections */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        // Handle item selection
        switch (item.getItemId()) {

            case R.id.itemReset:
                // Reset

                // Build
                DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which){
                        case DialogInterface.BUTTON_POSITIVE:
                            //Yes button clicked
                            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                            SharedPreferences.Editor editor = preferences.edit();
                            editor.clear();
                            editor.commit();
                            **// TODO: Refresh preferences**
                            break;

                        case DialogInterface.BUTTON_NEGATIVE:
                            //No button clicked
                            break;
                        }
                    }
                };

                // Display
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("Are you sure you want to reset your settings back to their default options?").setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener).show();

                // Return
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }

    }

    /* Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // Super
        super.onCreate(savedInstanceState);

        // Call
        addPreferencesFromResource(R.xml.preferences);
    }
}

Upvotes: 0

Views: 513

Answers (1)

Vinay
Vinay

Reputation: 2415

You should look at OnSharedPreferenceChangeListener.

It will give you the items which were changed hence you need to refresh all of them but update the changed.

Upvotes: 1

Related Questions