Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I dismiss AlertDialog manually in Android?

Why should I call dismiss() method for AlertDialog before Activity is destroyed? Something leaks, but what exactly? Why then PopupWindow can handle Activity destroying?

I've found in Android sources that each alert dialog creates window:

Window w = PolicyManager.makeNewWindow(mContext);

What does that mean? Why it cannot just use PhoneWindow obtained from activity?

ADDED

Say, AlertDialog references Context, Context references nothing, then GC should garbage collect both objects (as they are not referenced from 'outside'). What else has a reference to AlertDialog? In other words where is exactly memory leak?

like image 794
se.solovyev Avatar asked Mar 28 '13 13:03

se.solovyev


People also ask

How do I turn off AlertDialog on Android?

AlertDialog generally consists of the main title, the message, and two buttons, technically termed as a positive button and a negative button. Both positive and negative buttons can be programmed to perform various actions. By default, the negative button lets close the AlertDialog without any additional lines of code.

What is the use of AlertDialog in Android?

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.

How do I dismiss Materialalertdialogbuilder in Android?

setCancelable(false); AlertDialog dialog = builder. show(); In order to dismiss the dialog, you can call dismiss function like this.


1 Answers

Alertdialogs are attached to our activity using a id,This Link shows the activity of a alertdialog, once the alertdialog is displayed it is a seperate window (u set cancel false then u r forced to manage it) if it is running on back and your activity gets destroyed Every memory associated with it will be released including the id of the AlertDialog. Thats y the error pops up .I think its clear now.

like image 176
Viswanath Lekshmanan Avatar answered Oct 07 '22 04:10

Viswanath Lekshmanan