0

I'm creating an AlertDialog. Here's the snippet of code that I'm using:

final CharSequence[] items = {"Red", "Green", "Blue"};

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
        Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
    }
});
AlertDialog alert = builder.create();
alert.show();

This works fine but I haven't been able to figure out how save the selection option somewhere so that the next time the alert dialog is opened, the previously selected option is shown. I don't need this value to persist across application restarts, only as long as the application is in the background and not entirely discarded.

Being new to Android development, I have found this pretty difficult to accomplish.

Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382
  • 2
    This is a simple fundamental problem. This has NOTHING to do with android in particular. You basically just save the value just like any other variable... – JoxTraex Aug 27 '12 at 06:37
  • Save some flag value in sharedpreference and clear that preference when application destroy/relaunch. – Krishnakant Dalal Aug 27 '12 at 06:45
  • Hi JoaxTraex. You were right. I was not confused about the part of actually saving the value but mainly by the fact that I didn't know how to set the correct option when showing the dialog the next time. I missed the fact that `setSingleChoiceItems` allowed you to specify the pre-selected option. Most examples that I read to lean about building an `AlertDialog` showed the call to `setSingleChoiceItems` as `-1`. My snippet is an example of what I mean. I should have read the docs more thoroughly though. :-| – Mridang Agarwalla Aug 27 '12 at 12:10
  • @JoxTraex The thing is that the docs has this sentence "Although both a traditional list and a list with radio buttons provide a "single choice" action, you should use setSingleChoiceItems() if you want to persist the user's choice. That is, if opening the dialog again later should indicate what the user's current choice is, then you create a list with radio buttons" – Olumide Feb 28 '15 at 20:11

1 Answers1

4

Use the code below :

private static final String SELECTED_ITEM = "SelectedItem";
private SharedPreferences sharedPreference;
private Editor sharedPrefEditor;

private void showAlert() {
    final CharSequence[] items = {"Red", "Green", "Blue"};

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Pick a color");               
    builder.setSingleChoiceItems(items, getSelectedItem(), new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            saveSelectedItem(item);
            Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
        }       
    });
    AlertDialog alert = builder.create();
    alert.show();

}

private int getSelectedItem() {
    if (sharedPreference == null) {
        sharedPreference = PreferenceManager
                .getDefaultSharedPreferences(this);
    }
    return sharedPreference.getInt(SELECTED_ITEM, -1);
}

private void saveSelectedItem(int item) {
    if (sharedPreference == null) {
        sharedPreference = PreferenceManager
                .getDefaultSharedPreferences(this);
    }
    sharedPrefEditor = sharedPreference.edit();
    sharedPrefEditor.putInt(SELECTED_ITEM, item);
    sharedPrefEditor.commit();
}

For the first time, nothing will be selected. From second time onwards the item which is previously selected will be selected by default.

Eldhose M Babu
  • 14,382
  • 8
  • 39
  • 44