0

I have the following radio button dialog which works the way i want i also have size 12 set as the default as well but what i need to now be able to do is save the instancestate that is when something else is selected i want that size to be selected when the app is opened again. Here is my code

final CharSequence[] items = {"12m", "16m", "20m"};
AlertDialog.Builder builder = new AlertDialog.Builder(Tweaks.this);
builder.setTitle("Select a size");
builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
    if(items[item] == "12m"){
    Toast.makeText(this, "your size is 12", Toast.LENGTH_SHORT).show();
    }
    if(items[item] == "16m"){
    Toast.makeText(this, "your size is 16", Toast.LENGTH_SHORT).show();
    }
    if(items[item] == "20m"){
    Toast.makeText(this, "your size is 20", Toast.LENGTH_SHORT).show();
    }
}
})
    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
    dialog.cancel();
    }
}).show();

Thank you for any help

GFlam
  • 1,089
  • 4
  • 25
  • 38

2 Answers2

1

How to save the state of an Android CheckBox when the users exits the application?

However keep in mind this is a controlled save state. If your program should be killed due to lack of resources, you should save all appropriate info during onSaveInstanceState () and onRestoreInstanceState ()

Community
  • 1
  • 1
Ian
  • 3,390
  • 22
  • 24
0

Save the last selected instance state in perference file using SharedPreferences, then in your code always read the state from preference file when open the dialog, if no perference file existing, then use default.

Hong Ning
  • 953
  • 5
  • 6