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 :)
Related
I am using a dialog fragment to show loading message when app is launched for first time.
I am using public void show(FragmentManager manager, String tag) method to display the dialog.
This loading message will be dismissed after data loading is done by intent service.
I am using dismissAllowingStateLoss() to close the dialog.
I didnt use dismiss() method as the app was crashing sometimes. But with dismissAllowingStateLoss(), the dialog was not being dismissed sometimes ( very rarely once in 100 times).
I have searched many blogs about this issue, but couldnt get the root cause.
Does anyone face such issue or have any idea about this kind of issue. Please suggest
Have you tried something like this
getFragmentManager().beginTransaction().remove(yourFragment).commit();
If this doesnt solve the issue, please post some source-code so that we can try and assist you.
I am having one java class in that class as soon some one purchases our application then it will start downloading and progress dialog has to appear instead it goes to some other page and when i come out of the application and when i restart then it starts downloading.
Please Help me out from this mess...
Thank you
Check the condition for dialog, before showing.
Like this
if(pDialog!=null)
{
if(!pDialog.isShowing())
{
pDialog.show();
}
}
and also while removing the dialog in onPostexecute() check for null.
if Still not works just remove the pDialog and try once with your code.
Two causes for your error happen:
The error will happen if you're trying to show a Dialog after you've exited an Activity.
Also, if an unhandled Exception was thrown in your AsyncTask, which would cause the Activity to shutdown, then an open progress dialog will cause the Exception.
According to the Log you've posted, the error happens after you call pDialog.show() which might be the 1st cause I've mentioned before.
Also you are calling finish() in many parts of your code, maybe one of these calls are making your Activity to stop and leaking your Dialog.
You must check which one of them is finishing your Activity before you show the Dialog. A good solution is to dismiss the Dialog (if it's showing) before calling finish().
I need to show a yes/no dialog in AsyncTask.onPostExecute() but I keep getting
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
when the screen is rotated just before the dialog is shown. I had a similar problem showing dialogs in the onActivityResult but have since moved the dialogs to the
onPostResume() as suggested by other posts but I am unable to solve this one.
Is it possible to show a yes/no dialog in the onpostexecute() without causing exception and without using "commitAllowingStateLoss"?
Your help is much appreciated.
I think you should maintain state of your activity in which you are calling the dialog, because every time when activities orientation changes onCreate() function calls and if you are initiating any AsyncTask in it, then it will execute it again. May be that's why you are facing the problem. Just maintain its state and then check it, hope it will help you.
Just add this code in your manifest file for that activity. Just type landscape or portrait according to your need.
android:screenOrientation="portrait/landscape"
Thank you.
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 .
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.