Hi I designed a custom toolbar to replace the action bar with a popup menu, using the hints from
how to save menuitem visibility state through sharedpreferences?
and
Checkbox item state on menu android
and
http://developer.android.com/guide/topics/ui/menus.html#checkable
The most effective way is to store the state in shared preferences as in the stackoverflow answers.
My question is: How do I keep the checked option selected even after restarting my android?
One way is to call the
.clear()
method before.commit()
.Another is to retrieve the last stored value in shared preferences. However, to do this, one has to understand the lifecycle of an activity:
After storing the checked state of user preferences, as below: Declare variables
After setting the layout, assign the string (boolean or int in whatever your case may be) variable to shared preferences.
You have to retrieve the previously stored string from shared preferences, when the app is paused, stopped, resumed visible (started). So, in addition to the code above, you will need to add the following:
That is, if you really want to have the checked option stored at all times.