Android save instance state for dialog
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.OnC开发者_运维问答lickListener() {
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
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 ()
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.
精彩评论