Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am having 5 radiobuttons in my application and i want to save their states so that when i exit and then come back to the application then i see the same button clicked which was clicked prior to my exiting the application

Following is my code for saving the state

private int flag1 = true;
private void save(final boolean isChecked) {

    SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean("check", isChecked);
    editor.commit();
}

private boolean load() { 
    SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
    return sharedPreferences.getBoolean("check", true);
}

protected void onPause() {
    super.onPause();
    save(!flag1);
    SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
}

 @Override
 protected void onResume() {

    super.onResume();
    save(load());
        Toast.makeText(getApplicationContext(),""+load(), Toast.LENGTH_LONG).show();
}

I am calling the save method from inside the onCheckedChangeListener for every radiobutton but nothing happens.

Is there any other method of using sharedpreferences ??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
178 views
Welcome To Ask or Share your Answers For Others

1 Answer

you are saving all button state using same key(check). Try something like this

private void save(int radioid,final boolean isChecked) {

SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("check"+radioid, isChecked);
editor.commit();
}

In load method loop through all radiobutton

private void load() { 
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
for(int i=0;i<radioGroup.getChildCount();i++){
   RadioButton rbtn=(RadioButton)radioGroup.getChildAt(i);
   rbtn.setChecked(sharedPreferences.getBoolean("check"+rbtn.getId(), false)); 
 }
}

UPDATE

In RadioGroup only one button is checked at a time. so store check button id instead.

private void save(int radioid) {

SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("check", radioid);
editor.commit();
}

and use following code to restore

private void load() { 
   SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);

   int radioId=sharedPreferences.getInt("check", 0);
   if(radioId>0){
     RadioButton rbtn=(RadioButton)radioGroup.findViewById(radioId);
     rbtn.setChecked(true); 
   }

 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...