Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close an ActionMode menu programmatically on Honeycomb?

In my application there is a ListFragment where each item from the list contains a checkbox. Whenever the user clicks on one of those checkboxes the app starts an ActionMode context menu. But I want the application to close the ActionMode menu when clicking on another component. I tried Fragment#closeContextMenu() without success.

Any ideas how can I accomplish that?

like image 310
kaneda Avatar asked Jun 22 '12 15:06

kaneda


2 Answers

Whenever you are creating/starting ActionMode Create by

mMode = startActionMode(....); 

To Dismiss it use following Syntax

if (mMode != null)   {      mMode.finish();  } 
like image 50
Vipul Avatar answered Oct 02 '22 12:10

Vipul


Kotlin code

Use ActionMode.Callback to finish ActionMode after menu item pressed

 private val actionModeCallbacks = object : ActionMode.Callback {     override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {         mode.menuInflater.inflate(R.menu.menu_action_mode, menu)         return true     }      override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean {         return false     }      override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {         when (item.itemId) {             R.id.menu_item -> {                 mode.finish() // after click of menu item close action mode                 return true             }         }         return false     }      override fun onDestroyActionMode(mode: ActionMode) {       } } 

startSupportActionMode

(activity as AppCompatActivity).startSupportActionMode(actionModeCallbacks) 
like image 25
Kishore Jethava Avatar answered Oct 02 '22 13:10

Kishore Jethava