Discover that PopupMenu is closed at API level 11

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

I already tried this:

  • overrides the dismiss() method for PopupMenu, but it is not called when it is closed.
  • use PopupMenu.OnMenuItemClickListener , but it is not activated when the user clicked outside the menu (to close it) or clicked "back".

I have no other ideas to discover that the menu has been closed. Therefore, I hope someone has a smart trick. Otherwise, I cannot use PopupMenu ...

+6
source share
1 answer

You can try to override the method

 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) 

class PopupMenu . It is this method that calls setOnDismissListener() internally in API 14+. But to do this is not easy. The problem is that the com.android.internal.view.menu.MenuBuilder class is not included in the Android SDK, so you cannot compile your code if you try to import this class.

However, you can create your own β€œfake” implementation of this class, put it in the com.android.internal.view.menu package, make a .jar file with it, copy this β€œfake library” to the libs folder and set the scope of this library to IntelliJ IDEA project settings as provided. This means that the classes in this jar file already have real devices (which is true), and you only need to compile your code for them. The basic idea here is that your fake MenuBuilder should be in the classpath to compile javac , but should not be included in the resulting .apk file.

This is a difficult decision, but it should work.

+1
source

Source: https://habr.com/ru/post/927792/


All Articles