0

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

};
Randroid
  • 3,688
  • 5
  • 30
  • 55

1 Answers1

0

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

Community
  • 1
  • 1
breadbin
  • 584
  • 1
  • 11
  • 19
  • Thanks for the reply, I did what u said, but its not working. – Randroid Dec 17 '12 at 12:31
  • Why is the isclick variable static? Your onClick method's if statement seems to be functioning strangely, it sets isclick to true if it's already true, and your "else" sets isclick to false, even though it must already be false. – breadbin Dec 17 '12 at 13:50
  • Thanks, i ll try removing the isclick=true and isclick=false inside onclick, on reboot I want to retrieve the saved state of the buttons, Do I need any other changes in my code? – Randroid Dec 18 '12 at 04:08
  • I think the fact that your isclick variable is static may be causing a problem, though I might be wrong. I would try making it non-static, anyway. – breadbin Dec 18 '12 at 08:46
  • I even declared the isclick variable globally as private boolean isclick = false; still not working – Randroid Dec 18 '12 at 08:51
  • I have edited my answer asking another couple of questions. I would try putting in some logs every time you read/write to/from SharedPrefs, and in your onClick method. That way you can check if the right value is being written to SharedPrefs at all. – breadbin Dec 18 '12 at 09:02
  • Thanks for the suggestion, I have modified my code as u suggested. plz check my question(EDIT section) which I edited again. still i could nt able to save the button state. for the first time when I run I am getting the button with highlighted background and even on reboot also i am getting the same highlited background. plz suggest any modifications if needed. – Randroid Dec 18 '12 at 09:16
  • Try using onPause instead of onStop. I think onStop is not guaranteed to be called - but onPause is. – breadbin Dec 18 '12 at 09:39
  • I already tried with onpause() also, but the same old result(not working). on reboot and also if i run the application for the first time its always showing the highlighted background. and the thing is that on debugging the application the else part of my code i.e else if (!isclick) { seatdirnbtn.setBackgroundResource(R.drawable.icon4); } is not executing. I mean the control is not going here – Randroid Dec 18 '12 at 09:50
  • So your logs are telling you that isclick is false, but the "else if (!isclick)" is never evaluated as true? Have you tried specifiying "isclick == false" instead of "!isclick"? – breadbin Dec 18 '12 at 10:51
  • on debugging, isclick is showing as true in if-else condition. I tried "isclick == false" also. still I dont understand the problem.I think isclick is always returning true. – Randroid Dec 18 '12 at 10:57
  • So your isclick variable is always true. You need to figure out why. I don't understand the problem either, but if I were you I would investigate any code that changes the value of isclick. Try commenting out any change and see what happens, or change it in a different way. – breadbin Dec 18 '12 at 11:00