Android Persistent Checkable Menu in Custom Widget

2019-01-12 12:24发布

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?

1条回答
放荡不羁爱自由
2楼-- · 2019-01-12 12:43

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:

enter image description here

After storing the checked state of user preferences, as below: Declare variables

/**Checkable Login Persist Shared Prefs Declarations Start*/
private static final String PREFS_NAME = "IsCheckedState";
String string;
SharedPreferences.Editor editor;
/**Checkable Login Persist Shared Prefs Declarations End*/

After setting the layout, assign the string (boolean or int in whatever your case may be) variable to shared preferences.

protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     /**
      Persistent Checkable Menu Start
      **/
     SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
     string = settings.getString("preference", string);
     //Log.e("User Subscription", string);
     /**
      Persistent Checkable Menu End
      **/}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    SharedPreferences settings = this.getSharedPreferences(PREFS_NAME, 0);
    string = settings.getString("preference", string);
    if (string.equals("Vibrate")) {
        menu.findItem(R.id.start_action).setChecked(true);
        Log.e("Vibrate", string);
    }
    else if (string.equals("Disable")){
        menu.findItem(R.id.my_cancel_action).setChecked(true);
        Log.e("Disable", string);
    }
    return true;
}
// Menu options to set and cancel the alarm.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    /*persistent checkable item logic start*/
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
    editor = settings.edit();
    string = settings.getString("preference", string);
     /*persistent checkable item logic end*/
    int id = item.getItemId();
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    switch (id) {

        // When the user clicks START ALARM, set the alarm.
        case R.id.start_action:
            alarm.setAlarm();
            item.setChecked(true);
            string= "Vibrate";
            editor.putString("preference", string);
            editor.commit();
            // invalidateOptionsMenu();
            return true;
        // When the user clicks CANCEL ALARM, cancel the alarm.
        case R.id.my_cancel_action:
            alarm.cancelAlarm(this, 1);
            item.setChecked(true);
            string="Disable";
            editor.putString("preference", "Disable");
            editor.commit();
            //invalidateOptionsMenu();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

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:

}
@Override
public void onResume() {
    super.onResume();
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
    string = settings.getString("preference", string);
}

@Override
protected void onStart() {
    super.onStart();
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
    string = settings.getString("preference", string);
}

@Override
protected void onRestart() {
    super.onRestart();
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
    string = settings.getString("preference", string);
}


@Override
protected void onPause() {
    super.onPause();
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
    string = settings.getString("preference", string);
}
@Override
protected void onStop() {
    super.onStop();
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
    string = settings.getString("preference", string);
}

That is, if you really want to have the checked option stored at all times.

查看更多
登录 后发表回答