Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 503
Damn Vegetables Avatar asked Feb 16 '26 20:02

Damn Vegetables


1 Answers

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.

like image 196
itotsev Avatar answered Feb 18 '26 20:02

itotsev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!