Search code examples
androidandroid-activitysettingsrefreshpreferences

Refresh activity if preferences was changed


I have a PreferenceActivity with settings for my app and I want to refresh activity if some preferences was changed.

For example I have a checkbox which responsible for addtional tab in my layout, when checkbox is checked tab must be shown. To catch this event I used OnSharedPreferenceChangeListener. But this listener listen every click on this checkbox and do something (your logic) every time, every click even though setting really was not changed. I want to refresh my activity only if setting was really changed. If earlier was "OFF" and now become "ON" in this case I want to refresh, but if I changed OFF - ON - OFF I don't want to refresh.

How to catch really changes and how to handle this in activity?

Thanks!


Solution

  • How to catch really changes and how to handle this in activity?

    Step #1: Implement the OnSharedPreferenceChangeListener on your main activity.

    Step #2: Maintain a boolean data member, initially false, which you toggle whenever your checkbox of interest is changed.

    Step #3: In onStart() or onResume(), if the boolean data member is true, refresh the activity and set the data member to false.