Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 676
Georgy Gobozov Avatar asked Dec 01 '25 17:12

Georgy Gobozov


1 Answers

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.

like image 132
CommonsWare Avatar answered Dec 03 '25 09:12

CommonsWare



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!