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 .
Related
I have got this code in my Async task to go to a new activity when doInBackgound is completed, but no matter what I do, I always get the window leak error, I have also tried adding delay to the the progress bar, but with no success. Can anyone help me? Thanks
Problem is your Activity is finished and the ProgressDialog is theoretically still "displaying" which is not possible (since the Activity where it is displayed is gone), hence this error occurs. To prevent it remove the Handler around the dialog dismiss then it should work :)
Is there a method to return the working activity's context?
I started a process in the background, that need to make a toast in the middle of its execution, I need to have the current context so the toast can take it. The problem is I don't know exactly in what activity the user could be at the time the async task create the toast.
Any help please?
I need to have the current context so the toast can take it.
Application context of your app is sufficient for Toast to show. You do not need Activity's context (and Activity is subclass of Context btw) for this.
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.
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()
}
I have an activity with in which there is a async task that will do some download stuff. AT the time of downlaoding it will show a loading dialog.
My problem is, it worked fine for me when me doing it in only one orentiaon. But when i rotate at the time of download, it shows window leaked and will crash at the
dialog.cancel in my post excute.
From my study on it more i understood it due the change in the context when device is rotated.
That is when a device is rotated the activity will be recreated so the context will be changed.
But i have created the dialog with old one and that wasn't the current context. So when i cancel it it shows error
What is the solution for this, any idea frnds.
Me using honeycomb, me tried but with fragment but didnt get a good sample for that. Me now mainly trying that,
if anyone can give me some links for that it will be
great
First of all: open your dialog using the showDialog method (there are a lot of examples in the official documentation). If you do so, the activity will take care of dismissing the dialog on destroy, and re-showing it after the activity has been recreated.
Also... if the dialog shows a progress bar (not a wheel), you will want to update the progress of the dialog after orientation changes. In order to do so, I recommend to use the onRetainNonConfigurationInstance to return the current state of the dialog and/or the activity itself. Then, you can use getLastNonConfigurationInstance to recover that state. Google about those two methods if you want to see examples.
Another thing to keep in mind: if you are updating the state of the dialog an/or any other UI element from the AsyncTask, you must be aware that after the activity is recreated, the AsyncTask may be pointing to the wrong UI references. In order to handle this, you can create a proxy class (Proxy design pattern) to detach the AsyncTask progress notifications from the current UI elements.