5

I have an Implementation of ActionMode to display the number of multi selected items in a RecyclerView.

I would like to know when the back button in the actionMode is tapped so as to correspondingly reset the recyclerView but while implementing the ActionMode.Callback, i noticed that onDestroyActionMode is called whenever ActionMode is updated thus actionMode?.setTitle($selectedItems.size), which makes it impossible reset the recyclerView - remove selected items, remove overlay color and notify the recyclerview of data set changed. Mulltiselection

Here's my Callback

inner class ActionModeCallback : ActionMode.Callback {
    override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean {
        when (item?.getItemId()) {
            R.id.action_delete -> {
                myAdapter?.deleteSelectedIds()
                actionMode?.setTitle("") //remove item count from action mode.
                actionMode?.finish()
                return true
            }
        }
        return false
    }

    override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
        val inflater = mode?.getMenuInflater()
        inflater?.inflate(R.menu.action_mode_menu, menu)
        return true
    }

    override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean {
        menu?.findItem(R.id.action_delete)?.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
        return true
    }

    override fun onDestroyActionMode(mode: ActionMode?) {
        Log.d(TAG, "onDestroyActionMode Called")
        //myAdapter?.selectedIds?.clear()
        //myAdapter?.notifyDataSetChanged()
        actionMode = null
    }
}

How Can i know when the ActionMode back button is tapped? Full source code Here =>https://github.com/Edge-Developer/RecyclerViewMultiSelectExample

1 Answer 1

4

Holy Gosh. It was my fault, i was starting a New ActionMode each time an item is selected (through an interface on MainActivity) instead of checking if it has already been started before starting a new One.

Here was my code

actionMode = startActionMode(ActionModeCallback())

Here is the updated code

if (actionMode == null) actionMode = startActionMode(ActionModeCallback())

now, the onDestroyActionMode is called only once, after the actionMode is dismissed!

You can check the github repo on how's it was implemented

This problem was faced while implementing multiselection on a recyclerView.

1
  • What is the use of onPrepareActionMode? When it is called and what should we generally do there. I have gone through the docs but didn't get it right. Dec 9, 2020 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.