为按钮$ P $共享preferences pssed内tabhost不工作​​的重新启动状态 [英] Shared preferences for button pressed state inside tabhost not working on reboot

查看:177
本文介绍了为按钮$ P $共享preferences pssed内tabhost不工作​​的重新启动状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

里面tabhost共享preferences不工作的按钮pressed状态。

我改变的pressed状态按钮的背景。但是,当我重​​新启动(和关闭)的共享preferences不节电状态的手机。

变量

  btn_state

在下面的线总是返回false

 最终布尔btn_state = prefs.getBoolean(BUTTON_STATE,isclick);

任何帮助总是AP preciated,谢谢

这是我的code

 私人共享preferences preFS;
私人字符串prefName =我的preF;
私人共享preferences.Editor编辑;
私有静态最后弦乐BUTTON_STATE =button_selected;    @覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);    preFS = preferenceManager
            .getDefaultShared preferences(getApplicationContext());
    最终布尔btn_state = prefs.getBoolean(BUTTON_STATE,isclick);
    编辑= prefs.edit();
       如果(btn_state == FALSE){
        sea​​tdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
    }
    否则,如果(btn_state ==真){
        sea​​tdirnbtn.setBackgroundResource(R.drawable.icon4);
    }
      }
     @覆盖
公共无效的onStop(){
    super.onStop();
    preFS = preferenceManager
            .getDefaultShared preferences(getApplicationContext());    编辑= prefs.edit();
            editor.putBoolean(BUTTON_STATE,isclick);
    editor.commit();
}
      公共静态布尔isclick = FALSE;
私人View.OnClickListener listner1 =新View.OnClickListener(){    公共无效的onClick(视图v){        如果(isclick){
            sea​​tdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
            isclick = TRUE;            preFS = preferenceManager
                    .getDefaultShared preferences(getApplicationContext());
            编辑= prefs.edit();            editor.putBoolean(BUTTON_STATE,isclick);
            editor.commit();
        }其他{
            sea​​tdirnbtn.setBackgroundResource(R.drawable.icon4);
            isclick = FALSE;
        }        !isclick = isclick;
    }

修改

 私人布尔isclick;           @覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);      如果(savedInstanceState == NULL){isclick = FALSE; }    preFS = this.getShared preferences(prefName,MODE_PRIVATE);    isclick = prefs.getBoolean(prefName,FALSE);
    的System.out.println(?布尔+ isclick);    如果(isclick){
        sea​​tdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
    }否则如果(!isclick){
        sea​​tdirnbtn.setBackgroundResource(R.drawable.icon4);
    }
            }
           @覆盖
公共无效onRestart(){
    super.onRestart();
    如果(isclick){
        sea​​tdirnbtn.setBackgroundResource(R.drawable.icon4);
    }否则如果(!isclick){
        sea​​tdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
    }
}@覆盖
公共无效的onStop(){
    super.onStop();    preFS = this.getShared preferences(prefName,MODE_PRIVATE);
            编辑= prefs.edit();
        editor.putBoolean(prefName,真正的);
    editor.commit();
}         私人View.OnClickListener listner1 =新View.OnClickListener(){    公共无效的onClick(视图v){        如果(isclick){
            sea​​tdirnbtn.setBackgroundResource(R.drawable.icon4hlt);            编辑= prefs.edit();
            editor.clear();
            editor.putBoolean(prefName,真正的);
            editor.commit();
        }否则如果(!isclick){
            sea​​tdirnbtn.setBackgroundResource(R.drawable.icon4);
            编辑= prefs.edit();
            editor.clear();
            editor.putBoolean(prefName,FALSE);
            editor.commit();
        }        !isclick = isclick;
    }};


解决方案

我会用getShared preferences()而不是getDefaultShared preferences()。据我见过的StackOverflow的几个问题,如这个,可以有一些麻烦默认preferences方法。

您可以用getShared preferences(),这可能会帮助你解决这个问题更明确地指定preference文件。退房的Andr​​oid开发者共享preferences指南为code的例子和解释,如果你想尝试一下,应该帮助你。

有几件事情c您已经列出了我没有在$ C $明白。
你有2个布尔变量,和的onclick btn_state。为什么不直接使用吗?
btn_state被声明为final。为什么?
你已经宣布BUTTON_STATE字符串,但您不引用您的preFS使用它,你可以使用字符串BUTTON_STATE。

Shared preferences inside tabhost not working for button pressed state.

I am changing the background of the button on pressed state. But when I reboot(off and on) the phone the shared preferences is not saving the state.

The variable

       btn_state

in the below line is always returning False

 final boolean btn_state = prefs.getBoolean("BUTTON_STATE", isclick);

Any help is always appreciated,Thanks

here is my code

private SharedPreferences prefs;
private String prefName = "MyPref";
private SharedPreferences.Editor editor;
private static final String BUTTON_STATE = "button_selected";

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    prefs = PreferenceManager
            .getDefaultSharedPreferences(getApplicationContext());
    final boolean btn_state = prefs.getBoolean("BUTTON_STATE", isclick);
    editor = prefs.edit();
       if(btn_state == false){
        seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
    }
    else if(btn_state == true){
        seatdirnbtn.setBackgroundResource(R.drawable.icon4);
    } 
      }


     @Override
public void onStop() {
    super.onStop();
    prefs = PreferenceManager
            .getDefaultSharedPreferences(getApplicationContext());

    editor = prefs.edit();
            editor.putBoolean("BUTTON_STATE", isclick);
    editor.commit();


}
      public static boolean isclick = false;
private View.OnClickListener listner1 = new View.OnClickListener() {

    public void onClick(View v) {

        if (isclick) {
            seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
            isclick = true;

            prefs = PreferenceManager
                    .getDefaultSharedPreferences(getApplicationContext());
            editor = prefs.edit();

            editor.putBoolean("BUTTON_STATE", isclick);
            editor.commit();
        } else {
            seatdirnbtn.setBackgroundResource(R.drawable.icon4);
            isclick = false;
        }

        isclick = !isclick;
    }

EDIT

        private boolean isclick ;

           @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

      if (savedInstanceState == null) { isclick = false; }

    prefs = this.getSharedPreferences(prefName, MODE_PRIVATE);

    isclick = prefs.getBoolean("prefName", false);
    System.out.println("bool? " + isclick);

    if (isclick) {
        seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
    } else if (!isclick) {
        seatdirnbtn.setBackgroundResource(R.drawable.icon4);
    } 
            }


           @Override
public void onRestart() {
    super.onRestart();
    if (isclick) {
        seatdirnbtn.setBackgroundResource(R.drawable.icon4);
    } else if (!isclick) {
        seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
    }
}

@Override
public void onStop() {
    super.onStop();

    prefs = this.getSharedPreferences(prefName, MODE_PRIVATE);
            editor = prefs.edit();
        editor.putBoolean("prefName", true);
    editor.commit();


}

         private View.OnClickListener listner1 = new View.OnClickListener() {

    public void onClick(View v) {

        if (isclick) {
            seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);

            editor = prefs.edit();
            editor.clear();
            editor.putBoolean("prefName", true);
            editor.commit();
        } else if (!isclick) {
            seatdirnbtn.setBackgroundResource(R.drawable.icon4);
            editor = prefs.edit();
            editor.clear();
            editor.putBoolean("prefName", false);
            editor.commit();
        }

        isclick = !isclick;
    }

};

解决方案

I would use getSharedPreferences() instead of getDefaultSharedPreferences(). According to a few questions I've seen on StackOverflow, such as this one, there can be some trouble with the default preferences method.

You can specify preference files more explicitly with getSharedPreferences(), which might help you resolve this issue. Check out the Android Developers Shared Preferences guide for code samples and explanations that should help you if you want to try it.

There are a few things I don't understand in the code you've listed. You have 2 boolean variables, onclick and btn_state. Why not just use one? btn_state is declared as final. Why? You have declared a BUTTON_STATE String, but you don't use it in referencing your prefs, you use the string, "BUTTON_STATE".

这篇关于为按钮$ P $共享preferences pssed内tabhost不工作​​的重新启动状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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