-2

I have WindowManager: android.view.WindowLeaked in stacktrace and I have to handle it.

I know reasons why it appears but fixing it will take too much time.

All I need to catch it like typical exception, something like:

try {} catch (WindowLeaked e) {}

But that doesn't work for sure because it's not Exception.

Is there any way to do it?

I don't ask how to fix it or what is WindowLeaked.

The question is: How can I know that WindowLeak happened?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Andrey Danilov
  • 6,194
  • 5
  • 32
  • 56
  • You need to include the code which is causing window leak – waqaslam Apr 06 '17 at 09:11
  • Possible duplicate of [Activity has leaked window that was originally added](http://stackoverflow.com/questions/2850573/activity-has-leaked-window-that-was-originally-added) – shadygoneinsane Apr 06 '17 at 09:15
  • @shadygoneinsane I dont ask how to fix it. I ask how to **handle it when it happened** – Andrey Danilov Apr 06 '17 at 09:20
  • @waqaslam I dont need because its theoretical question. I want to know how can I catch WindowLeaked in code for **every** possible situation when its happened, not how to fix it in my case. – Andrey Danilov Apr 06 '17 at 09:21
  • `How can I know that WindowLeak happened?` What's the purpose of that? Assuming you knew, what will you do then? – azizbekian Apr 06 '17 at 09:22
  • @azizbekian in my case I have some actions when dialog appears and some actions when it dismissed. So dialog opened -> some actions done -> WindowLeak -> dialog doesnt exist but actions before dismiss are not executed, All I need to handle WindowLeak and execute them manually. – Andrey Danilov Apr 06 '17 at 09:28
  • If you find yourself in a situation, when `WindowLeak` happens, it means you have to redesign your app in the way that it won't happen, and do not come the other way around. It's not an exception that should be caught. – azizbekian Apr 06 '17 at 09:29
  • @azizbekian as I said I know reason why I have WindowLeak, In my case its because Activity is not exists when I try to show dialog after javaRx response. But I have to do tons of checks to fix it. If I catch WindowLeak result will be the same but I will spend less time. – Andrey Danilov Apr 06 '17 at 09:29
  • The answer to your question is that your application cannot "know" when it has happened, and it cannot handle it. You (the programmer) can know that it has happened (by reading the logcat!) but there is nothing you can do about it ... apart from tracking down the root cause and fixing it. – Stephen C Apr 06 '17 at 10:01
  • @shadygoneinsane WindowLeaked is **not** throws as exception, thats the problem! – Andrey Danilov Apr 06 '17 at 11:23
  • @DEADMC oh yeah i almost forgot -- so your only option here would be that inside your `onStop()` or `onPause()` and `onDestroy()` make sure you dismiss your dialog or if you added a view using window Manager then remove it . – shadygoneinsane Apr 06 '17 at 11:42

2 Answers2

1

The answer to your question is that your application cannot "know" when it has happened. Despite its unconventional name, android.view.WindowLeaked is a Java exception. However, it is uncatchable because it not thrown in any context where there is no application code on the call stack.

(Apparently, it is not thrown at all. Rather, it seems to be used as a means of capturing a stacktrace for logging. This does seem rather odd since the stack trace doesn't tell you much ... given that there is no application code on it!)

You (the programmer) can know that it has happened (by reading the logcat!) but there is nothing you can do about it ... apart from tracking down the root cause and fixing it.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • The problem is WindowLeaked is not even thrown. Its created only to show it`s stacktrace to log =( But looks like you are right, there is no way to handle WindowLeaked in code. The one way is analyse logcat while application works and send event after WindowLeaked appeared, but its madness =( – Andrey Danilov Apr 06 '17 at 12:13
0

Window Leak exception usually occurs when the user have open the view and didn't close the view.

paras08
  • 21
  • 1
  • I wrote " I know reasons why it appears but fixing it will take too much time." The question is how can I know that it happened in code. – Andrey Danilov Apr 06 '17 at 09:20
  • The only way is that app will crash at some-point when the task will run in the background. And Window leak exception will make the app crash – paras08 Apr 06 '17 at 11:26
  • WindowLeak cannot make app crash because it doesnt throws as usual exception -.- – Andrey Danilov Apr 06 '17 at 11:42
  • Sometimes window leak make the app crash if we doesn't throw an exception for example if we doesn't close the dialogue it lead the app crash. – paras08 Apr 06 '17 at 12:10