25

Look at these pieces of code:

Custom views and window attributes on Android

Problem

When I click 'Home button', exception is thrown: Activity has leaked window... from this line:

localWindowManager.addView(colourView, layoutParams);

Question(s)

Do you know what can cause it?

Problem doesn't occur, when I close application with back button.

Exception/Error Logs

W/InputManagerService(   96): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@40908148 (uid=10056 pid=1368)
D/CordovaActivity( 1368): CordovaActivity.onDestroy()
D/CordovaWebView( 1368): >>> loadUrlNow()
E/WindowManager( 1368): Activity com.phonegap.helloworld.HelloWorld has leaked window pl.edu.uj.tcs.student.xxx.Display$Layer@40589368 that was originally added here
E/WindowManager( 1368): android.view.WindowLeaked: Activity com.phonegap.helloworld.HelloWorld has leaked window pl.edu.uj.tcs.student.xxx.Display$Layer@40589368 that was originally added here
E/WindowManager( 1368):         at android.view.ViewRoot.<init>(ViewRoot.java:258)
E/WindowManager( 1368):         at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
E/WindowManager( 1368):         at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
E/WindowManager( 1368):         at android.view.Window$LocalWindowManager.addView(Window.java:424)
E/WindowManager( 1368):         at pl.edu.uj.tcs.student.xxx.Display.setColorsViews(Display.java:181)
E/WindowManager( 1368):         at pl.edu.uj.tcs.student.xxx.Display$3.run(Display.java:139)
E/WindowManager( 1368):         at android.os.Handler.handleCallback(Handler.java:587)
E/WindowManager( 1368):         at android.os.Handler.dispatchMessage(Handler.java:92)
E/WindowManager( 1368):         at android.os.Looper.loop(Looper.java:130)
E/WindowManager( 1368):         at android.app.ActivityThread.main(ActivityThread.java:3683)
E/WindowManager( 1368):         at java.lang.reflect.Method.invokeNative(NativeMethod)
E/WindowManager( 1368):         at java.lang.reflect.Method.invoke(Method.java:507)
E/WindowManager( 1368):         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/WindowManager( 1368):         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/WindowManager( 1368):         at dalvik.system.NativeStart.main(Native Method)

D/CordovaActivity( 1368): onMessage(onPageStarted,about:blank)
D/CordovaWebViewClient( 1368): onPageFinished(about:blank)
D/CordovaActivity( 1368): onMessage(onPageFinished,about:blank)
D/CordovaActivity( 1368): onMessage(exit,null)
I/power   (   96): *** set_screen_state 0

EDIT:

How can I add something to onPause(), onStop() etc. functions in Cordova Activity?

EDIT 2:

Why is that a problem? Because all I create is class that extends CordovaPlugin and small auxilary classes. That's all. I'm not able (I suppose) to modify Activity class body. All I can do is get reference to it by calling cordova.getActivity() function.

Community
  • 1
  • 1
tomwesolowski
  • 956
  • 1
  • 11
  • 27

5 Answers5

48

What is a leak in programming?

The memory that you acquire and do not release lead to the memory leak.Similar happens with the (windows/dialogs).

What's happening here?

You are trying to add a window and while it shows up it is on the foreground,but when you are pressing the home button it gets paused and then gets stopped (Try to put a toast in onStop() and onPause()).

Since you did not tell the system to remove your view , hence it remains attached to the window that now has disappeared/detached from the application. Hence according to the system your customView occupied the space which it did not release.

Solution

Inside your onStop() or onPause()andonDestroy() make sure you dismiss your view(dismiss() if it's a dialog) or remove it(remove()if added using window Manager).

Add the dismiss or remove functions inside your on unload function as you mentioned that on pressing back button you get this error.On exiting an app its onUnload() method gets called.

Suggestion(Ignore if not in context)

As i can observe you are trying to make a System alert window that comes over anything beneath it.Adding such kind of pop ups in the activity is risky as it may cause leakage problems. You may actually add such kind of window via a Service so it outlives your activity and shows up everywhere on the device (if that is what you need).

Check this out

Update 2-The Cordova lifecycle

Why don't you try to override onUnload method in your CordovaPlugin class.I tried finding but the docs are mentioning the existence of onPause and onResume methods.If you got onUnload in the CordovaPlugin class then remove the view you are making in your view class runOnUiThread method.

cafebabe1991
  • 4,928
  • 2
  • 34
  • 42
  • No offense but I did not understand your edit note.It seems ambiguous to me.Please be more specific so that i can help you out easily. – cafebabe1991 Feb 25 '14 at 02:33
  • Can you post your complete code of activity and cordova class – cafebabe1991 Feb 27 '14 at 05:18
  • https://github.com/mrfesol/DisplayPlugin/blob/master/src/pl/edu/uj/tcs/student/tomaszwesolowski/Display.java – tomwesolowski Feb 27 '14 at 14:58
  • why don't to try to override onUnload method in your CordovaPlugin class.I tried finding but the docs are mentioning the existence of onPause and onResume methods.If you got onUnload in the CordovaPlugin class then remove the view you are making in your view class runOnUiThread method. – cafebabe1991 Feb 27 '14 at 16:57
  • Oh, I didn't notice it before. Thank you. If you post an answer within next 15 hours, you'll get a bounty. – tomwesolowski Feb 27 '14 at 22:03
11

You're trying to show a Dialog after you've exited an Activity.

The solution is to call dismiss() on the Dialog you created before exiting the Activity, e.g. in onPause(). All windows&dialogs should be closed before leaving an Activity.

@Override
 protected void onStop() {
  super.onStop();
  if (loadingDlg != null) {
   loadingDlg.dismiss();
   loadingDlg = null;
  }
}

Hope it should helpful for you.

Activity-has-leaked-window-that-was-originally-added

Community
  • 1
  • 1
Jebasuthan
  • 5,538
  • 6
  • 35
  • 55
  • For the record, I've had this issue before. Due to the timing of the dialog being displayed via a callback it is possible there is nothing you can do other then to catch the exception when trying to display the dialog and squelch it (unless you want to attempt to synchronize all callbacks with the status of the activity). –  Feb 26 '14 at 04:21
  • https://github.com/mrfesol/DisplayPlugin/blob/master/src/pl/edu/uj/tcs/student/tomaszwesolowski/Display.java – tomwesolowski Feb 27 '14 at 14:56
3

Check weather the dialog is showing or not

@Override
  protected void onStop() {
    super.onStop();
if (loadingDlg != null) {

        if(loadingDlg.isShowing())
        loadingDlg.dismiss();

        loadingDlg = null;
 }
}
Karthick pop
  • 616
  • 3
  • 16
0

Leaked window usually happens when your context show dialogs and that context is suddenly forced close that your dialog has not been dismissed properly.

In order to fix this, you have to fix your errors before the window has leaked error.

Rick Royd Aban
  • 904
  • 6
  • 33
0

I had a finish() that was called place before my AlertDialog was called which caused the Activity to finish before the AlertDialog was called. I removed the finish() and put it after the user had completed their input using the AlertDialog and the window leak was fixed.

JanB
  • 904
  • 9
  • 14