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.

refereed from here

Shared preferences for button pressed state inside tabhost not working on reboot

on debugging, isclick is showing as true in if-else condition. I dont understand the problem.

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.

Any help is always appreciated.

private boolean isclick ;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    seatdirnbtn = (Button) findViewById(R.id.seatdirnbtn);
    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;
    }
};

EDIT1:

    private boolean isclick  ;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    prefs = this.getSharedPreferences(prefName, MODE_PRIVATE);

    isclick = prefs.getBoolean("prefName", true);

    System.out.println("bool? " + isclick);
    Log.i(TAG, " prefname");

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

         public void onPause() {
    super.onPause();
    prefs = this.getSharedPreferences(prefName, MODE_PRIVATE);
    // boolean isclick = false;
    editor = prefs.edit();
    // editor.clear();
    editor.putBoolean("prefName", false);
    Log.i(TAG, " prefname");
    editor.commit();
}

@Override
public void onRestart() {
    super.onRestart();
      prefs = this.getSharedPreferences(prefName, MODE_PRIVATE);
        isclick = prefs.getBoolean("prefName", !isclick);

}

@Override
public void onStop() {
    super.onStop();
    getApplicationContext().unbindService(this);

      prefs = this.getSharedPreferences(prefName, MODE_PRIVATE); // boolean
      isclick = false; editor = prefs.edit(); // editor.clear();
      editor.putBoolean("prefName", !isclick); 
      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", false);
            editor.commit();
            isclick = false;
        } else if (!isclick) {
            seatdirnbtn.setBackgroundResource(R.drawable.icon4);
            editor = prefs.edit();
            editor.clear();
            editor.putBoolean("prefName", true);
            editor.commit();
            isclick = true;
        }

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

1 Answers1

2

Try with this Edited Solution :

public class SharedprefsActivity extends Activity  {

    protected static final String TAG = "HvacActivity";
    /** Called when the activity is first created. */
    private Button seatdirnbtn;
    private SharedPreferences prefs;
    private String prefName = "MyPref";
    private SharedPreferences.Editor editor;
    private boolean isclick;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        seatdirnbtn = (Button) findViewById(R.id.seatdirnbtn);      
        seatdirnbtn.setOnClickListener(listner1);       
    }

    public void onResume() {
        super.onResume();
        getPrefAndButtonState();        
    }

    public void onPause() {
        super.onPause();
        setPrefAndButtonState();
    }

    @Override
    public void onRestart() {
        super.onRestart();
        getPrefAndButtonState();
    }

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

    public void getPrefAndButtonState(){
        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);
        }
    }

    public void setPrefAndButtonState(){
        editor = prefs.edit();
        editor.putBoolean("prefName", isclick);
        editor.commit();        
        getPrefAndButtonState();
    }

    private View.OnClickListener listner1 = new View.OnClickListener() {    
        public void onClick(View v) {   
            if (isclick) {
                isclick = false;
                setPrefAndButtonState();            
            } else if (!isclick) {
                isclick = true;
                setPrefAndButtonState();
            }   
        }
    };
}

I have test well and it works well for me.

Hope it helps you.

Thanks.

Pratik Sharma
  • 13,307
  • 5
  • 27
  • 37
  • Thanks for the reply , but on reboot I am not getting the saved state of the button image.if the button is clicked the background image with highlited should be saved and retrieved on reboot – Randroid Dec 18 '12 at 12:09
  • @Capricorn Try with the edited solution I have posted to solve your issue. – Pratik Sharma Dec 18 '12 at 12:15
  • Thanks for the code , but when I added your piece of code I couldn't able to click on the button after I run the application.I should click it to change the background image so that the pressed state with highlight background image should get saved in the shared preferences. any Idea? – Randroid Dec 18 '12 at 13:36
  • @Capricorn it will be good if you send some small sample app to me for this issue. That will be good and easy to make it solve. You can remove all other codes from your app and send with the required code to me. I will debug here and send back to you with proper solution. Thanks. – Pratik Sharma Dec 19 '12 at 04:44
  • Thanks again, my problem is solved a bit. i.e on reboot its working fine and I am getting the pressed state image for my button(highlited background image when pressed for first time, normal background image when pressed for second time). and the the which I am facing right now is when I run the application for the first time I am getting the highlited background image for the button. I should have normal background on my first run. I ll post the code plz check my edit1 – Randroid Dec 19 '12 at 06:12
  • I have added the edit1, plz check once and I think the problem could be with oncreate(), as I am calling this if (isclick) { seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt); } else if (!isclick) { seatdirnbtn.setBackgroundResource(R.drawable.icon4); } , coz of this I think on first run its showing the highlited background for the button. Thanks again – Randroid Dec 19 '12 at 06:21
  • @Capricorn See the solution I have posted with "Edit 1". – Pratik Sharma Dec 19 '12 at 06:29
  • @ Pratik Sharma, Thanks,but if I press back button and run the application again, then I am getting the highlited background as usual like earlier. – Randroid Dec 19 '12 at 06:48
  • @Capricorn It looks like flow of flag is not proper. You are storing `!isclick` in pref in onStop() method. So it will reverse the effect made during run time and app stop. – Pratik Sharma Dec 19 '12 at 06:51
  • @ Pratik Sharma,SO, Do I need any changes? – Randroid Dec 19 '12 at 06:58
  • @Capricorn follow the same pattern everywhere if `true` then `isclick` and if `false` then `!isclick`. That will solve your problem for sure. – Pratik Sharma Dec 19 '12 at 07:02
  • everywhere means? I didnt get pratik. – Randroid Dec 19 '12 at 07:04
  • @Capricorn means in your `Activity` from starting to end. – Pratik Sharma Dec 19 '12 at 07:05
  • I tried with this condition in my oncreate , if(prefs.getBoolean("prefName", true)){ isclick = true; } else{ isclick = false; } is this condition what have u suggested? still no result pratik – Randroid Dec 19 '12 at 10:09
  • @Capricorn if possible wrap your code with required files and email me I will correct it and send back to you. – Pratik Sharma Dec 19 '12 at 10:11
  • Thanks ,If so,may I have your mail id? – Randroid Dec 19 '12 at 10:13
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/21374/discussion-between-pratik-sharma-and-capricorn) – Pratik Sharma Dec 19 '12 at 10:14
  • Hi pratik, If I save the preferences for more than one button then how can I call the saved state in oncreate(),Do I need to use an array for more than one button.Thanks. – Randroid Jan 09 '13 at 04:49
  • @Raghav yes you need to maintain them separately. Like For Preferences "prefName1","prefName2","prefName3","prefName4" and for click state isclick1,isclick2,isclick3,isclick4 like this. – Pratik Sharma Jan 09 '13 at 15:57