Search code examples
androidandroid-activitycontextmenuclicklistener

How to dismiss a ContextMenu without the Back button?


The application has a contextmenu opened and I want to close/dismiss it without using the back button. Is it possible to receive a click event somewhere so I can call close?

closeContextMenu ()

Or can I call

setCancelOnTouchOutside()

somewhere like I can on a Dialog? Alternative for me is to refactor all my context menu's to PopupWindows which can also do

setOutsideTouchable()

Anyone a good hint/idea?


Solution

  • dispatch a key event (back)and you're done.

    myView.getRootView().dispatchKeyEvent(new KeyEvent (KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));