Android ProgressDialog: Leaked window

activity has leaked window android.widget.popupwindow that was originally added here
android.view.windowleaked activity has leaked window decorview
windows leaked android
activity has leaked window decorview that was originally added here android
leaked window android widget popupwindow popupdecorview
android.view.windowleaked activity dialog
unable to add window -- token null is not for an application
windowmanager android view windowleaked activity has leaked

I've got an Activity with a Tabhost, all tabs are represented by Activities, one of them called BrowserTab.

All these Activities observe an Observable class, Data.

In BrowserTab, I call a method in Data, which retrieves some data from the internet. I want to notify the user with a ProgressDialog on calling this method. I tried it like this:

In BrowserTab:

public void loadXML(){
    progressDialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);
    data.loadXML(getUrl());
}

public void update(Observable o, Object arg)
{
    progressDialog.dismiss();
}

In Data:

public void loadXML(String url)
{
    this.xml = new XMLParser().parse(url);
    setChanged();
    notifyObservers();

}

Like this, the ProgressDialog appears áfter loading, to dissapear again immediately.

When I create a new Thread in the loadXML(String url) method in Data, I get a "Activity has leaked window" exception when the Observers are notified.

I've searched for solutions to this problem, but I just can't figure out how to fix this. Anyone an idea?

Old answer:: Please check the last edit

The answer is use this.getApplicationContext() instead of this:

progressDialog = ProgressDialog.show(this.getApplicationContext(), "", "Loading. Please wait...", true);

more explanation is here:

ProgressDialog : how to prevent Leaked Window

Edit

Well I found out later that this.getApplicationContext()

always is equal to null, So I used DialogFragment and add a ProgressBar to it instead of using the ordinary ProgressDialog.

Edit 2 this answer is better as it solves the root cause. Which is that the window that hosts the dialog gets dismissed before the dialog, so simply ask the activity to dismiss the dialog before being dismissed

Android says leaking windows on progress dialog · Issue #842 , Android says leaking windows on progress dialog #842. Closed. akmal opened this issue on Nov 23, 2015 · 1 comment. Closed  probably App is crashing be because you are trying to update UI element from doInBackground so use onPostExecute for updating ui elements instead of doInBackground – ρяσѕρєя K May 27 '13 at 8:13

Try to change your code with this..

progressDialog = ProgressDialog.show(getParent(), "", "Loading. Please wait...", true);

SigninActivity.java can leak the ProgressDialog window · Issue #200 , Activity has leaked window that was originally added - Android device: which causes the Activity to shutdown, then an open progressdialog causes this  Leaked window Exception When using Progress Dialog in Android Views have a reference to their parent Context (taken from constructor argument). If you leave an Activity without destroying Dialogs and other dynamically created Views, they still hold this reference to your Activity (if you created with this as Context: like new ProgressDialog(this)), so it cannot be collected by the GC, causing a memory leak.

I always use for this issue a C++ based style approach. So, you should use a WeakReference (In C++ you have TWeakObjectPtr) of the progress Dialog. Using a WeakReference inside a separated thread, asyncTask, etc.. ensures you that if some actor as GC clear your reference, WeakReference.get() would return null. Therefore you can infere that you are about to work with a leaked window...

This is how it works. We can illustrate an asyncTask based constructor as follows...

    private WeakReference<ProgressDialog> progressDialog;
    private final FinishListener listener;

    public SaveDataTask(ProgressDialog progressDialog,
                         @NonNull FinishListener listener) {
        this.progressDialog = new WeakReference<>(progressDialog);
        this.listener = listener;
    }
    ...
    @Override
    protected void onPostExecute(Boolean success) {
        if (progressDialog.get() != null) {
            progressDialog.get().cancel();
        }
        listener.onSaveFinish()
   }

Hope it helps

Activity has leaked window that was originally added, leaked window com.android.internal.policy.impl.PhoneWindow$DecorView that was originally added here error handling in Android, Android leaked window A common error that occurs when you use AsyncTask to perform a background task (e.g. download a file or fetch data from a remote server) with a progress dialog to display the task progress is leaked window .

Leaked window Exception When using Progress Dialog in Android, myP has leaked window com.android.internal.policy.impl. Activity to shutdown, then an open progressdialog caused this. Exception.. so the  opus1269 changed the title GoogleSigninActivity.java can leak the ProgressDialog window SigninActivity.java can leak the ProgressDialog window Apr 10, 2016 This comment has been minimized. Sign in to view

Activity has leaked window that was originally added, WindowLeaked: Activity com.example.android has leaked window Here mProgress is an instance of ProgressDialog and getResponse() is a  Activity has leaked window that was originally added - Android device: Emulator Nexus 5x API 22 with Play Services Android OS version: 5.1. Toggle sidebar TUTORIALS

Activity has leaked window, progressdialog and context - window leaked - android. I even know that window leak occurs if i m trying to show dialog on a context which is finished. How did I find out? Well, just like @Bobby says in a comment beneath the accepted answer, just scroll further up to your logs and you'll see the first reason or event that really threw the Exception. Apparently, the message "Activity has leaked window that was originally added" is only an Exception that resulted from whatever the first