Skip to content Skip to sidebar Skip to footer

Android Persistent Checkable Menu In Custom Widget After Reboot Android

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

Solution 1:

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*/privatestaticfinalString PREFS_NAME = "IsCheckedState";
Stringstring;
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.

protectedvoidonCreate(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
      **/}
@OverridepublicbooleanonPrepareOptionsMenu(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);
    }
    elseif (string.equals("Disable")){
        menu.findItem(R.id.my_cancel_action).setChecked(true);
        Log.e("Disable", string);
    }
    returntrue;
}
// Menu options to set and cancel the alarm.@OverridepublicbooleanonOptionsItemSelected(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();returntrue;
        // 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();returntrue;
    }
    returnsuper.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:

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

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

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


@OverrideprotectedvoidonPause() {
    super.onPause();
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
    string = settings.getString("preference", string);
}
@OverrideprotectedvoidonStop() {
    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.

Post a Comment for "Android Persistent Checkable Menu In Custom Widget After Reboot Android"