Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android AlertDialog setOnDismissListener for API lower than 17

I created an AlertDialog:

private CharSequence[] _items = { "item1", "item2", "item3", "item4", "item5", "item6", "item7" };  AlertDialog.Builder daysBuilder = new AlertDialog.Builder(this);     daysBuilder.setTitle("SomeCaption");      daysBuilder.setMultiChoiceItems(_items,new Boolean[] { false, true, false,  false false false, true }, SetListener);     daysBuilder.setPositiveButton("OK", OKListener);     daysBuilder.setNegativeButton("Cancel", CancelListener);      AlertDialog alert = daysBuilder.create();     alert.show();` 

Through the statement "new Boolean[] { false, true, false false false false, true }" the items in the dialog get checked/unchecked by default.

When I open the dialog, change the selection of the items but then dismiss (by pressing cancel or the back-button of the device) the dialog gets dismissed. So far so good.

But when I reopen the dialog, the items have the checked/unchecked state of the previous changes from the last opening of the dialog.

But when the dialog was dismissed at the first opening, I want to have the items checked/unchecked state like when I created the dialog (new Boolean[] { false, true, false false false false, true }).

So basically I need an opportunity to get notified when the dialog gets dissmissed so I can then reset the state of the items.

I tried it with the setOnDismissListener for the dialog object. Unfortunately this is just available in API 17.

setOnDismissListener worked perfect for me (exactly what I need) in the emulator (API 17) but not on my device (Android 4.1 => API 16)

Is there something similar in API 16?

like image 493
Eudaimonie Avatar asked Dec 18 '12 12:12

Eudaimonie


People also ask

Is AlertDialog deprecated?

A simple dialog containing an DatePicker . This class was deprecated in API level 26.

What is the use of AlertDialog in Android?

Alert Dialog shows the Alert message and gives the answer in the form of yes or no. Alert Dialog displays the message to warn you and then according to your response the next step is processed. Android Alert Dialog is built with the use of three fields: Title, Message area, Action Button.

What is AlertDialog message?

AlertDialog. A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout. DatePickerDialog or TimePickerDialog. A dialog with a pre-defined UI that allows the user to select a date or time.

Which method is used to set in AlertDialog?

The way to make a checkbox list is to use setMultiChoiceItems . // setup the alert builder AlertDialog. Builder builder = new AlertDialog. Builder(context); builder.


1 Answers

The problem is you are using setOnDismissListener of AlertDialog.Builder. This was introduced in Api level 17, setOnDismissListener of AlertDialog itself has been since api level 1.

AlertDialog alert = daysBuilder.create(); alert.setOndismissListener(yourdismisslistener); alert.show();` 
like image 83
nandeesh Avatar answered Sep 21 '22 13:09

nandeesh