重新启动 Android 后,自定义小部件中的 Android 持久性可检查菜单 [英] Android Persistent Checkable Menu in Custom Widget After Reboot Android

查看:10
本文介绍了重新启动 Android 后,自定义小部件中的 Android 持久性可检查菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计了一个自定义工具栏,使用来自

的提示用弹出菜单替换操作栏

存储用户偏好的勾选状态后,如下图:声明变量

/**Checkable Login Persist Shared Prefs Declarations Start*/private static final String PREFS_NAME = "IsCheckedState";字符串字符串;SharedPreferences.Editor 编辑器;/**Checkable Login Persist Shared Prefs Declarations End*/

设置布局后,将字符串(无论您的情况是布尔值还是整数)变量分配给共享首选项.

protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);/**持久可检查菜单开始**/SharedPreferences 设置 = PreferenceManager.getDefaultSharedPreferences(this);string = settings.getString("preference", string);//Log.e("用户订阅", string);/**持久化可检查菜单结束**/}@覆盖公共布尔 onPrepareOptionsMenu(菜单菜单){super.onPrepareOptionsMenu(menu);SharedPreferences 设置 = this.getSharedPreferences(PREFS_NAME, 0);string = settings.getString("preference", string);if (string.equals("Vibrate")) {menu.findItem(R.id.start_action).setChecked(true);Log.e("振动", 字符串);}else if (string.equals("Disable")){menu.findItem(R.id.my_cancel_action).setChecked(true);Log.e("禁用", string);}返回真;}//设置和取消闹钟的菜单选项.@覆盖公共布尔 onOptionsItemSelected(MenuItem item) {/*持久化可检查项逻辑开始*/SharedPreferences 设置 = PreferenceManager.getDefaultSharedPreferences(this);编辑器 = settings.edit();string = settings.getString("preference", string);/*持久化可检查项逻辑结束*/int id = item.getItemId();//在此处处理操作栏项目的点击.操作栏将//自动处理 Home/Up 按钮上的点击,这么长时间//当您在 AndroidManifest.xml 中指定父活动时.开关(ID){//当用户点击START ALARM时,设置闹钟.案例 R.id.start_action:警报.setAlarm();item.setChecked(true);字符串=振动";editor.putString("preference", string);editor.commit();//invalidateOptionsMenu();返回真;//当用户点击 CANCEL ALARM 时,取消闹钟.案例 R.id.my_cancel_action:警报.cancelAlarm(this, 1);item.setChecked(true);字符串=禁用";editor.putString("preference", "Disable");editor.commit();//invalidateOptionsMenu();返回真;}返回 super.onOptionsItemSelected(item);}

当应用暂停、停止、恢复可见(启动)时,您必须从共享首选项中检索先前存储的字符串.因此,除了上面的代码之外,您还需要添加以下内容:

<代码>}@覆盖公共无效 onResume() {super.onResume();SharedPreferences 设置 = PreferenceManager.getDefaultSharedPreferences(this);string = settings.getString("preference", string);}@覆盖受保护的无效 onStart() {super.onStart();SharedPreferences 设置 = PreferenceManager.getDefaultSharedPreferences(this);string = settings.getString("preference", string);}@覆盖protected void onRestart() {super.onRestart();SharedPreferences 设置 = PreferenceManager.getDefaultSharedPreferences(this);string = settings.getString("preference", string);}@覆盖受保护的无效 onPause() {super.onPause();SharedPreferences 设置 = PreferenceManager.getDefaultSharedPreferences(this);string = settings.getString("preference", string);}@覆盖受保护的无效 onStop() {super.onStop();SharedPreferences 设置 = PreferenceManager.getDefaultSharedPreferences(this);string = settings.getString("preference", string);}

也就是说,如果您真的希望始终存储选中的选项.

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

/**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.

这篇关于重新启动 Android 后,自定义小部件中的 Android 持久性可检查菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆