Android prevent leaked windows - android

While running monkey tool on my app, I am getting android.view.WindowLeaked exception, I referred some stackoverflow threads and found that we need avoid this while creating popups, instead we need to use getApplicationContext() but this is causing BadTokenException
E/AndroidRuntime(5597): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
Again after searching through I found this answer which says
One cause of this error may be trying to display an application
window/dialog through a Context that is not an Activity
So how to solve this?

Usually window leaks because You don't use showDialog method in Activity but only using Dialog.show() method.
When dialog is displayed and configuration of Your Activity changes (eg. orientation change) dialog has not window to attach, and this results in leak of window.
If You use Compatibility library You should use DialogFragment instead of pure dialog.

the reason behind WindowManager$BadTokenException is that you keep showing dialog or popupwindow on the window whose context is not alive now.
So should always dismiss popup or dialog whenever you switch between activities or dismiss any activity.
Therefore you should probably call dilog.dismiss() in onPause method.
onPause()
{
dilog.dismiss()
}

Related

Android Dialog + Leak Canary

I was trying to use built-in AlertDialog with onDismiss listener (without it result was same) and custom AppCompatDialog subclass. After dialog dismissed, current activity should finish. But few seconds later memory leak push appears. If I put breakpoint in custom dialog's onStop() method (delay is don't matter), this memory leak caption is not shown. Can it be fixed in normal way? Maybe, someone already faced with a similar problem?
This memory leak was related to displaying toast with current activity's context. When I injected application context into class, that displaying toasts, leak was fixed

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?
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.

progressdialog and context - window leaked

I am currently trying to define a seperate class for displaying ProgressDialogs as i dont want to create individual progressDialog instances in each and every activity. And i m currently sending the current activities context to that method. Things seems to work fine but at times (very random) it leads to a exception stating window Leaked. I even know that window leak occurs if i m trying to show dialog on a context which is finished. And i think the issue here could be because of some other part of code which is finishing my context before my dialog is shown.so i just want to make sure my method doesn't show the dialog if there is any issue with context.
Is there any way i can know whether my passed context is currently visible on the screen or not so that i will not run into these window leaked exceptions.
And also tried replacing the context with getApplicationContext(). but the progress dialog is not shown at all with this change.
Any help would be greatly appreciated.
Thank you all in advance
Dismiss the dialog in onPause() method .

Is there a way to kill all the visible dialog as an activity is destroyed?

I am asking this because I am always getting the Activity has leaked window.. error
Couldn't figure out a way to destroy some of the dialogs on activity destroy since these dialogs
are dynamically generated outside the activity(on some listener).
So is there a way for android to detect and kill all the visible/live dialogs?
Thanks
I believe that you can keep a member reference to your dialogs as you create them. Then you can check the isShowing() method to see if its showing. Then hide and destroy the dialogs that are showing.

nested AlertDialogs in android

I'm trying to display an AlertDialog inside of another AlertDialog. When the user clicks on the any item within the initial AlertDialog another AlertDialog is created and shown.
I'm following the correct pattern for creating and displaying AlertDialogs, the problem is as soon as the code reaches the point where the innerDialog.show() method is encountered the application fails. The logcat prints an uncaught runtime exception:
android.view.WindowManager$BadTokenException : Unable to add window -- token null is not for an applicaion
I'm wondering if i'm allowed to call the show() method on the innerAlertDialog manually.
The outer AlertDialog is working because i'm using the callback onCreateDialog() method.
In previous versions there was a bug where getApplicationContext() returned null.
I still dont quite if it has been fixed however with dialogs, its always better to send this.
And for the main question, its working as designed to avoid locking the UI thread.
I have seen people recommending creating a new layout with the theme of the dialog and start the activity on the first dialog, however I havent try this yet.

Categories

Resources