Damn Vegetables
Damn Vegetables

Reputation: 12474

Rotating while PopupMenu is showing, gets android.view.WindowLeaked

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?

Upvotes: 3

Views: 1089

Answers (1)

itotsev
itotsev

Reputation: 121

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.

Upvotes: 3

Related Questions