Search code examples
androidpreferencespreferencepreferencefragment

Keep Android Preferences From "Remembering" State


I'm using a PreferenceFragment to provide a clear, consistent layout to my users. However, for this particular screen, I need the information to always be clear/fresh/blank/reset each time it's visited.

For example: The user enters some text on the EditTextPreference (let's say "mText") in my PreferenceFragment xml view. When the user exits the screen and comes back to the same PreferenceFragment, I need NOT for "mText" to show up in the EditTextPreference when they click it. Currently, it is showing up. Or say the user checks a CheckBoxPreference. When they come back, it needs to be unchecked again (the default value/state).

I understand that I could manually set the values in code to what I want them to be (i.e. blank/reset). However, this seems tedious and not like the correct way to do so.


Solution

  • In your fragment's onPause() method you can do:

    @Override
    public void onPause() {
        super.onPause();
        mEditTextPreference.getEditor().clear().apply();
    }