-1

I develop an Android App. One module is to retrieve data from remote server on button click. While retreiving it should display progress spin and After retrieving it should disappear. The code is given below

  b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            new Load_data().execute();

        }
    });
        }

 private class Load_data extends AsyncTask<Void, Void, Void> {
    ProgressDialog pd;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd= ProgressDialog.show(result.this, "Retreiving", "Please Wait....");
    }

    protected Void doInBackground(Void... param) {
        publishProgress(param);
        butt();
        return null;
    }

    protected void onPostExecute(String arg) {
        pd.dismiss();
    }
}

The Error i got is given below

09-09 18:22:39.422: E/WindowManager(524): android.view.WindowLeaked: Activity com.example.covai.result has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{37ab2005 V.E..... R......D 0,0-684,324} that was originally added here
09-09 18:22:39.422: E/WindowManager(524):   at android.view.ViewRootImpl.<init>(ViewRootImpl.java:422)
09-09 18:22:39.422: E/WindowManager(524):   at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:250)
09-09 18:22:39.422: E/WindowManager(524):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
09-09 18:22:39.422: E/WindowManager(524):   at android.app.Dialog.show(Dialog.java:297)
09-09 18:22:39.422: E/WindowManager(524):   at android.app.ProgressDialog.show(ProgressDialog.java:116)
09-09 18:22:39.422: E/WindowManager(524):   at android.app.ProgressDialog.show(ProgressDialog.java:99)
09-09 18:22:39.422: E/WindowManager(524):   at android.app.ProgressDialog.show(ProgressDialog.java:94)
09-09 18:22:39.422: E/WindowManager(524):   at com.example.covai.result$Load_data.onPreExecute(result.java:337)
09-09 18:22:39.422: E/WindowManager(524):   at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
09-09 18:22:39.422: E/WindowManager(524):   at android.os.AsyncTask.execute(AsyncTask.java:535)
09-09 18:22:39.422: E/WindowManager(524):   at com.example.covai.result$3.onClick(result.java:138)
09-09 18:22:39.422: E/WindowManager(524):   at android.view.View.performClick(View.java:4471)
09-09 18:22:39.422: E/WindowManager(524):   at android.view.View$PerformClick.run(View.java:18784)
09-09 18:22:39.422: E/WindowManager(524):   at android.os.Handler.handleCallback(Handler.java:808)
09-09 18:22:39.422: E/WindowManager(524):   at android.os.Handler.dispatchMessage(Handler.java:103)
09-09 18:22:39.422: E/WindowManager(524):   at android.os.Looper.loop(Looper.java:193)
09-09 18:22:39.422: E/WindowManager(524):   at android.app.ActivityThread.main(ActivityThread.java:5330)
09-09 18:22:39.422: E/WindowManager(524):   at java.lang.reflect.Method.invoke(Native Method)
09-09 18:22:39.422: E/WindowManager(524):   at java.lang.reflect.Method.invoke(Method.java:372)
09-09 18:22:39.422: E/WindowManager(524):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
09-09 18:22:39.422: E/WindowManager(524):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)

2 Answers2

0

The leak comes probably from your PixelRainActivity.staticThis attribute. If you’re keeping a reference to an activity, even after that this activity has been destroyed, you have a memory leak.

The easiest way to fix is to use the application’s Context instead. Change your staticThis = this line in the method onCreate() to staticThis = this.getApplicationContext() and it should work (and change the type of staticThis to Context if this is not already the case)

show progress dialog

if (pd != null && !pd.isShowing() && !activity.isFinishing(){
    pd.show();
}

hide progress dialog

if (pd != null && pd.isShowing()) {
  pd.dismiss();
}
Nooruddin Lakhani
  • 7,507
  • 2
  • 19
  • 39
0

You are creating progress dialog in wrong manner, you need to create it like below.

ProgressDialog pd;
pdia1 = new ProgressDialog(Youractivity.this);
            pdia1.setMessage("Loading Quiz...");
            pdia1.setCancelable(false);
            pdia1.setCanceledOnTouchOutside(false);
            pdia1.show();
karan
  • 8,637
  • 3
  • 41
  • 78