Shared preferences for button pressed state inside

2019-09-15 16:19发布

问题:

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;
    }

};

回答1:

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".