I am getting WindowLeakedError
while trying to show ProgressDialog
in onPreExecute
in AsyncTask
. Some Stackoverflow threads have mentioned there could be the case that Activity
is finishing
and I try to show ProgressDialog
in same Activity
this might cause this exception but I have put a check that if activity is in
finishing state`, dont show it.
WindowLeaked Exception
05-23 10:43:27.528: E/WindowManager(21108): Activity com.example.MyActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40dd0968 that was originally added here
05-23 10:43:27.528: E/WindowManager(21108): android.view.WindowLeaked: Activity com.example.MyActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40dd0968 that was originally added here
05-23 10:43:27.528: E/WindowManager(21108): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:351)
05-23 10:43:27.528: E/WindowManager(21108): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:373)
05-23 10:43:27.528: E/WindowManager(21108): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:321)
05-23 10:43:27.528: E/WindowManager(21108): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:152)
05-23 10:43:27.528: E/WindowManager(21108): at android.view.Window$LocalWindowManager.addView(Window.java:541)
05-23 10:43:27.528: E/WindowManager(21108): at android.app.Dialog.show(Dialog.java:301)
05-23 10:43:27.528: E/WindowManager(21108): at com.triplebottomline.ezmsds.web.FetchDataTask.onPreExecute(FetchDataTask.java:47)
05-23 10:43:27.528: E/WindowManager(21108): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
05-23 10:43:27.528: E/WindowManager(21108): at android.os.AsyncTask.execute(AsyncTask.java:511)
05-23 10:43:27.528: E/WindowManager(21108): at com.example.MyActivity$4.onClick(MyActivity.java:467)
Please see the following code:
AsyncTask Class
public class FetchDataTask extends AsyncTask
{
private Activity callerActivity;
private ProgressDialog dialog;
public FetchDataTask (Activity activity)
{
callerActivity = activity;
}
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(callerActivity);
dialog.setCancelable(false);
if(callerActivity != null && !callerActivity.isFinishing())
{
dialog.show();
}
}
@Override
protected void onPostExecute(Object result) {
dialog.dismiss();
dialog = null;
}
Whole Activity
code is too large. But I am posting the code which is triggering the AsyncTask
on Button
Click
AsyncTask Caller Code
FetchDataTask task = new FetchDataTask(MSDSCategories.this);
task.execute(TaskType.DOWNLOAD_PDF);
Please help and point me out what can be the issue.
Thanks in advance.
Regards,