Activity is closed before alert dismisses
<p>In the below given code i am getting the error "Activity is closed before Alertdialog dismisses and gives Activity Leaked window exception" and I am getting the exception in alert.show in onProgressUpdate. What is the solution for this ?</p>

<pre><code> String values[] = { Login.uname, Login.pass };
                    new Communicator().execute(values);

    class Communicator extends AsyncTask&lt;String, String, String&gt; 
        {
        int prog = 0;

        @Override
        protected String doInBackground(String... params) {
            String response = null;
            try {
                if(Login.uname != null &amp;&amp; Login.pass != null){
                    sync(Login.uname, Login.pass);
                }else if(SplashScreen.user != null &amp;&amp; SplashScreen.pas != null){
                    sync(SplashScreen.user, SplashScreen.pas);
                }
                if("ok".equalsIgnoreCase(status))
                {
//                  else{
                    if(db != null)
                    {
                        if (Login.uname != null) {
                            usernam = Login.uname;
                        } else if (SplashScreen.user != null) {
                            usernam = SplashScreen.user;
                        }
                        Cursor cursor = db.getAllMixes(SplashScreen.user);
                        if(cursor!=null &amp;&amp; cursor.getCount() &gt; 0)
                        {
                            Log.e("&gt;&gt;&lt;&lt;", "&gt;&gt;cursor.getCount()&lt;&lt;"+cursor.getCount());
                        }
                    }
                    count ++;
//                  }
                }
                totalsize = 0;
                for(int i=0; i&lt;list.size(); i++)
                {
                    totalsize += Integer.parseInt(cont.get(i).getSize()); 
                }

            } catch (Exception e) {
                Log.e("Data.LOG", e.getMessage(), e);

            } finally {
            }
            return response;
        }

        @Override
        protected void onPreExecute() {
            prog = 0;
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            if ("ok".equalsIgnoreCase(status)) 
            {
                sizekb=0;
                textsync.setText("Syncing mix "+1+"/"+cont.size());
                if(cont.size() == 0){
                    progressBar2.setVisibility(View.INVISIBLE);
                    progress.setProgress(0);
                    textsync.setText("No Mixes on Server");
                    alertdial =new AlertDialog.Builder(NeliteMusicActivity.this).create();
                    alertdial.setTitle(R.string.set4Play);
                    alertdial.setMessage(getString(R.string.noMixes));
                    alertdial.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                    alertdial.setButton(getString(R.string.ok), 
                            new DialogInterface.OnClickListener() 
                    {
                          public void onClick(DialogInterface dialog, int which) {
                              alertdial.dismiss();
                              animatedStartActivity();
                        }
                        });
                    alertdial.setCanceledOnTouchOutside(false);
                    alertdial.show();
                }
            }if("tomany".equalsIgnoreCase(status))
            {
                progress.setVisibility(View.INVISIBLE);
                progressBar2.setVisibility(View.INVISIBLE);
                textsync.setVisibility(View.INVISIBLE);
                AlertDialog alert =new AlertDialog.Builder(NeliteMusicActivity.this).create();
                alert.setTitle(R.string.set4Play);
                alert.setMessage(getString(R.string.twoDevice));
                alert.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                alert.setButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        animatedStartActivity();
                    }
                });
                alert.setCanceledOnTouchOutside(false);
                alert.show();
            }
            if("fail".equalsIgnoreCase(status))
            {
                progress.setVisibility(View.INVISIBLE);
                progressBar2.setVisibility(View.INVISIBLE);
                textsync.setVisibility(View.INVISIBLE);
                AlertDialog alert =new AlertDialog.Builder(NeliteMusicActivity.this).create();
                alert.setTitle(R.string.set4Play);
                alert.setMessage(getString(R.string.connectionerror));
                alert.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                alert.setButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        animatedStartActivity();
                        return;
                    }
                });
                alert.setCanceledOnTouchOutside(false);
                alert.show();
            }
        }

        @Override
        protected void onProgressUpdate(String... values) {
            super.onProgressUpdate(values);
            int a = 0;
            if(!isChange){
                a=messag;
                progress.setProgress(a);
                textsync.setText("Syncing mix "+c+"/"+list.size());
            }else{
                progressBar2.setVisibility(View.INVISIBLE);
                progress.setProgress(0);
                textsync.setText("Syncing interrupted");
            }
            if(progress.getMax()&lt;=a)
            {
                        if (isOnline()) 
                        {
                            if(NeliteMusicActivity.alertShow==true){
                            final AlertDialog alert =new AlertDialog.Builder(NeliteMusicActivity.this).create();
                            alert.setTitle(R.string.set4Play);
                            alert.setMessage(getString(R.string.updatedMix));
                            alert.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                            alert.setButton(getString(R.string.ok), 
                                    new DialogInterface.OnClickListener() 
                            {
                                  public void onClick(DialogInterface dialog, int which) {
                                      alert.dismiss();
                                      animatedStartActivity();
                                }
                                });
                            alert.setCanceledOnTouchOutside(false);
                            if((alert1 ==null || !alert1.isShowing()) &amp;&amp; !alert.isShowing())
                            alert.show();
                            }
                        }
                    if (!isOnline()) {
                        if(NeliteMusicActivity.alertShow==true){
                        final AlertDialog alert =new AlertDialog.Builder(NeliteMusicActivity.this).create();
                        alert.setTitle(R.string.set4Play);
                        alert.setMessage(getString(R.string.connectionerror));
                        alert.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                        alert.setButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
                              public void onClick(DialogInterface dialog, int which) {
                                  alert.dismiss();
                                  animatedStartActivity();
                            } 
                            }); 
                        alert.setCanceledOnTouchOutside(false);
                        if(!alert.isShowing())
                        alert.show();
                        }
                    }
            }
        }
    }
</code></pre>

<p>Logcat:</p>

<pre><code>12-04 17:50:27.390: E/WindowManager(8190): Activity com.nelitemusic.NeliteMusicActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40a2cfb8 that was originally added here
12-04 17:50:27.390: E/WindowManager(8190): android.view.WindowLeaked: Activity com.nelitemusic.NeliteMusicActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40a2cfb8 that was originally added here
12-04 17:50:27.390: E/WindowManager(8190):  at android.view.ViewRoot.&lt;init&gt;(ViewRoot.java:291)
12-04 17:50:27.390: E/WindowManager(8190):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:249)
12-04 17:50:27.390: E/WindowManager(8190):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:193)
12-04 17:50:27.390: E/WindowManager(8190):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:118)
12-04 17:50:27.390: E/WindowManager(8190):  at android.view.Window$LocalWindowManager.addView(Window.java:532)
12-04 17:50:27.390: E/WindowManager(8190):  at android.app.Dialog.show(Dialog.java:269)
12-04 17:50:27.390: E/WindowManager(8190):  at com.nelitemusic.NeliteMusicActivity$Communicator.onProgressUpdate(NeliteMusicActivity.java:520)
12-04 17:50:27.390: E/WindowManager(8190):  at com.nelitemusic.NeliteMusicActivity$Communicator.onProgressUpdate(NeliteMusicActivity.java:1)
12-04 17:50:27.390: E/WindowManager(8190):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:606)
12-04 17:50:27.390: E/WindowManager(8190):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-04 17:50:27.390: E/WindowManager(8190):  at android.os.Looper.loop(Looper.java:132)
12-04 17:50:27.390: E/WindowManager(8190):  at android.app.ActivityThread.main(ActivityThread.java:4126)
12-04 17:50:27.390: E/WindowManager(8190):  at java.lang.reflect.Method.invokeNative(Native Method)
12-04 17:50:27.390: E/WindowManager(8190):  at java.lang.reflect.Method.invoke(Method.java:491)
12-04 17:50:27.390: E/WindowManager(8190):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
12-04 17:50:27.390: E/WindowManager(8190):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
12-04 17:50:27.390: E/WindowManager(8190):  at dalvik.system.NativeStart.main(Native Method)
</code></pre>
