1
votes

Hi i have button default it should be unselected,when i click it should be selected if i go next activity and if i come back it should be selected,if i was not selected before going next activity it should be unselected.how can i do this final Button up8 = (Button) findViewById(R.id.adultup8);
up8.setOnClickListener(new View.OnClickListener() {public void onClick(View view) { if(teeth[7]==0){up8.setBackgroundResource(R.drawable.adultup8); teeth[7]=8;}else{up8.setBackgroundResource(R.drawable.adultup8_pressed);teeth[7]=0;}}});

2

2 Answers

0
votes

put your code into onReume(). It will help you out definitely.

0
votes

Your Initialization should be in onCreate() or at class level. Performing condition check should be at onResume(). If this button has to be of application level performing this condition, then you should use shared preferences to hold boolean value, so that, the state of button can be saved at application level.

protected void onResume() {
// TODO Auto-generated method stub
super.onResume();

up8.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        if(teeth[7]==0){
            up8.setBackgroundResource(R.drawable.adultup8);
            teeth[7]=8;
        }
        else{
            up8.setBackgroundResource(R.drawable.adultup8_pressed);
            teeth[7]=0;
        }
    }
});

}