Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect that a PopupMenu is closed on API level 11

I have this PopupMenu in my application and would like to know when it is closed. With API14+ this is easy when adding a dismiss listener with setOnDismissListener(). But, I need to know when the PopupMenu is closed from API11+, so I cannot use the listener and need an alternative for the listener.

This I have tried already to:

  • override the dismiss() method of the PopupMenu, but it is not called when it is closed.
  • use the PopupMenu.OnMenuItemClickListener, but it is not activated when the user clicked outside the menu (to close it) or clicks 'back'.

I do not have any other ideas to detect that the menu was closed. So I am hoping that someone else has a clever trick. Otherwise I cannot use PopupMenu...

like image 258
Veger Avatar asked Oct 15 '12 22:10

Veger


1 Answers

You can try to override method

public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing)

of PopupMenu class. Exactly this method is calling setOnDismissListener() internally in API 14+. But this is not easy to do. Problem is what class com.android.internal.view.menu.MenuBuilder is not included to Android SDK, so you will not be able to compile your code if you will trying to import this class.

However, you can create your own "fake" implementation of this class, place it in package com.android.internal.view.menu, make a .jar file with it, copy this "fake library" to libs folder and set scope of this library in Project Settings of IntelliJ IDEA as "provided". This means that classes in this jar file already have real devices (what is true) and you only need them to compile your code. Main idea here - your fake MenuBuilder should be in classpath for javac compilation, but should not be included in resulting .apk file.

This is not easy solution, but it should work.

like image 53
HitOdessit Avatar answered Oct 12 '22 19:10

HitOdessit