2

I have created a simple custom layout by extending ViewGroup. I create a Button and a PopupMenu on init(). If the button gets pressed, the PopupMenu gets shown. The problem is that, when the PopupMenu is showing, if I rotate the device, I get the following error message.

E/WindowManager: android.view.WindowLeaked: Activity has leaked window android.widget.PopupWindow$PopupDecorView{c44e7da V.E...... ......ID 0,0-392,960} that was originally added here

I have found a similar question (PopupMenu PopupWindow$PopupViewContainer leak), and the solution was calling dismiss() of the PopupMenu onStop(). However, since this is not an activity but a ViewGroup, I have no onStop(). So, I tried to find a similarly named event, and found onDetachedFromWindow(). I called dismiss() in onDetachedFromWindow().

onDetachedFromWindow() does get called before screen rotation, but I still get the WindowLeaked error message.

How can I solve this problem? Actually, it seems the application works fine and all I get is the error message in the log. Is this error ignorable?

Community
  • 1
  • 1
Damn Vegetables
  • 11,484
  • 13
  • 80
  • 135

1 Answers1

2

Probably this answer is too late for you, but I'll share what I found out in case anyone else is interested. PopupMenu uses PopupWindow internally. When you call PopupMenu.dismiss() it calls PopupWindow.dismiss() but dismissing a PopupWindow is not immediate when an exit transition is specified. Despite the fact that you call dismiss() at the appropriate time, the window is not actually removed from the hierarchy fast enough. It needs to be removed before your activity gets destroyed in order not to get this leakage error. I don't see a means of dismissing the PopupMenu immediately. A possible workaround could be to use a PopupWindow directly instead of a PopupMenu, and call setExitTransition(null) before dismissing.

itotsev
  • 93
  • 1
  • 7
  • Implementing a popup menu from scratch from a PopupWindow seems to be a lot of work. Even though I see the exception message, there was no noticeable impact of it. So my question is if I can ignore this. Since the name contains "leak", does this mean if this exception happens the memory is not reclaimed? – Damn Vegetables Oct 20 '16 at 17:22
  • Well, I chose to ignore it. It seems not to happen on older Android versions before API 23. I hope Google are going to fix it soon. – itotsev Oct 26 '16 at 19:25