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