0

I have an app that returns search results. From the list of results, a specific item can be clicked and then a detail page appears. I am getting the following window leak error whenever I make a search and return the results, view a specific result's detail, go back to the list of results, and then click another to see the details. The error comes when I click the second result and the detail page is loading.

Here's the logcat of the error:

11-17 21:32:47.876: E/WindowManager(5945): Activity com.tforan.blobtag4.PlaceActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40707298 that was originally added here
11-17 21:32:47.876: E/WindowManager(5945): android.view.WindowLeaked: Activity com.tforan.blobtag4.PlaceActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40707298 that was originally added here
11-17 21:32:47.876: E/WindowManager(5945):  at android.view.ViewRoot.<init>(ViewRoot.java:278)
11-17 21:32:47.876: E/WindowManager(5945):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
11-17 21:32:47.876: E/WindowManager(5945):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
11-17 21:32:47.876: E/WindowManager(5945):  at android.view.Window$LocalWindowManager.addView(Window.java:433)
11-17 21:32:47.876: E/WindowManager(5945):  at android.app.Dialog.show(Dialog.java:265)
11-17 21:32:47.876: E/WindowManager(5945):  at com.tforan.blobtag4.PlaceActivity$FactualRetrievalTask.onPreExecute(PlaceActivity.java:703)
11-17 21:32:47.876: E/WindowManager(5945):  at android.os.AsyncTask.execute(AsyncTask.java:391)
11-17 21:32:47.876: E/WindowManager(5945):  at com.tforan.blobtag4.PlaceActivity.factualQuery(PlaceActivity.java:295)
11-17 21:32:47.876: E/WindowManager(5945):  at com.tforan.blobtag4.PlaceActivity.onCreate(PlaceActivity.java:217)
11-17 21:32:47.876: E/WindowManager(5945):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
11-17 21:32:47.876: E/WindowManager(5945):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
11-17 21:32:47.876: E/WindowManager(5945):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1873)
11-17 21:32:47.876: E/WindowManager(5945):  at android.app.ActivityThread.access$1500(ActivityThread.java:135)
11-17 21:32:47.876: E/WindowManager(5945):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
11-17 21:32:47.876: E/WindowManager(5945):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-17 21:32:47.876: E/WindowManager(5945):  at android.os.Looper.loop(Looper.java:150)
11-17 21:32:47.876: E/WindowManager(5945):  at android.app.ActivityThread.main(ActivityThread.java:4358)
11-17 21:32:47.876: E/WindowManager(5945):  at java.lang.reflect.Method.invokeNative(Native Method)
11-17 21:32:47.876: E/WindowManager(5945):  at java.lang.reflect.Method.invoke(Method.java:507)
11-17 21:32:47.876: E/WindowManager(5945):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
11-17 21:32:47.876: E/WindowManager(5945):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
11-17 21:32:47.876: E/WindowManager(5945):  at dalvik.system.NativeStart.main(Native Method)

Why exactly is this happening? What can I do to get rid of this error? Happy to provide further code as needed. Thanks.

EDIT - here is the code of the problematic AsyncTask:

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(PlaceActivity.this);
        dialog.setMessage("Loading...");
        dialog.setIndeterminate(true);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setCancelable(true);
        dialog.show();
    }

    @Override
    protected RowResponse doInBackground(RowQuery... query) {
        RowResponse resp = factual.fetchRow("restaurants", id, query[0]);
        return resp;
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
    }

    @Override
    protected void onPostExecute(RowResponse resp) {
        StringBuffer sb = new StringBuffer();
        String today = findCurrentDay(currentDay);
        JSONArray todayHours = null;
        Log.i("factual response", resp.toString());

        for (Map<String, Object> restaurant : resp.getData()) {

            //..a bunch of JSON parsing..

        dialog.dismiss();
    }
user2163853
  • 907
  • 4
  • 14
  • 24

1 Answers1

0

I guess you are showing dialog somewhere in the code and to create the dialog you are using the context of the activity. the window leak is happening only because you are finishing the activity while the dialog is being shown.

Make sure you are not doing the above thing while returning from the search.

Dinesh Prajapati
  • 9,274
  • 5
  • 30
  • 47
  • not sure why this is happening, because even though i run an asynctask in that activity that has a progressdialog, i dismiss the dialog upon completion of the asynctask. – user2163853 Nov 18 '13 at 06:02
  • 1
    What I mean is your activity is getting destroyed before dialog is shown or showing – Dinesh Prajapati Nov 18 '13 at 06:04
  • How could that be happening? – user2163853 Nov 18 '13 at 06:06
  • That is possible if you call the finish exactly after .execute of asynctask – Dinesh Prajapati Nov 18 '13 at 06:17
  • the finish/dismissal of the dialog, or the finish of the activity? – user2163853 Nov 18 '13 at 06:20
  • ok makes sense. another SO post basically says this can be avoided by calling .cancel() on the asynctask in the onPause() of the activity, or calling dismiss() on the dialog (within the asynctask) in the onPause() of the activity. how do I make those calls from the mainactivity? can you show me how to onPause() would be overridden? – user2163853 Nov 18 '13 at 06:24
  • If you go to android tutorial it's easy to find how to override onPause for activity. in that you can dismiss the dialog and even cancel the asynctask too – Dinesh Prajapati Nov 18 '13 at 07:17