Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The way you are using your dialog is not recommended anymore! You should consider using a <strong>DialogFragment</strong>.</p> <p>The problem why you are loosing your checked state is because the dialog is recreated when you open it again.</p> <p>See this example of the DialogFragment approach <a href="http://developer.android.com/reference/android/app/DialogFragment.html" rel="nofollow noreferrer">http://developer.android.com/reference/android/app/DialogFragment.html</a>. You will see that you can pass arguments to the dialog.</p> <p>For a solution I recommend that when you close the dialog you pass the selected value back to the hosting activity... when the dialog should be reopenned you just pass the arguments to the dialog so that the dialog sets its default selection.</p> <p><strong>Edit:</strong></p> <ol> <li><p>Since you would like to display checkboxes I will take and adapt the example code from <a href="http://developer.android.com/guide/topics/ui/dialogs.html#AddingAList" rel="nofollow noreferrer">http://developer.android.com/guide/topics/ui/dialogs.html#AddingAList</a> for checkboxes. It's still convenient to use the AlertDialog.Builder.</p></li> <li><p>Wrap the Builder into a DialogFragment by overwriting the onCreateDialog-method. You can pass the list of selected items via the Bundle as boolean array and then use is for setMultiChoiceItems. </p> <pre><code>public class CheckBoxAlertDialogFragment extends DialogFragment { public static CheckBoxAlertDialogFragment newInstance(boolean[] checkedItems) { CheckBoxAlertDialogFragment frag = new CheckBoxAlertDialogFragment(); Bundle args = new Bundle(); args.putBooleanArray("checkedItems", checkedItems); frag.setArguments(args); return frag; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final boolean[] checkedItems = getArguments().getBooleanArray("checkedItems"); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); // Set the dialog title builder.setTitle(R.string.pick_toppings) // Specify the list array, the items to be selected by default (null for none), // and the listener through which to receive callbacks when items are selected .setMultiChoiceItems(R.array.toppings, checkedItems, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { checkedItems[which] = isChecked; } }) // Set the action buttons .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { // User clicked OK, so save the checkedItems results somewhere // or return them to the component that opened the dialog //... } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { //... } }); return builder.create(); } } </code></pre></li> <li><p>In your activity you should have a variable, e.g. named checkedItems, of type boolean[] somewhere which saves the checkbox states. You can open the dialog then with: </p> <pre><code>void showDialog() { DialogFragment newFragment = CheckBoxAlertDialogFragment.newInstance(checkedItems); newFragment.show(getFragmentManager(), "dialog tag"); } </code></pre></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload