Some questions re sample project ActionModeList2


#1

Hello,
I’m trying to understand the implementation of ActionMode using RecyclerView and am kind of stuck at the sample project ActionModeList2 in chapter Advanced RecyclerView under the head Changing the Contents (Pg 1537).
Here are some questions in this regard. I found it better to keep all questions under the same thread.

  1. In MainActivity, is it necessary to reassign activeMode=mode; in onCreateActionMode() when activeMode has already been assigned a value in overridden method onChecked() via activeMode=startActionMode(this); which actually triggered the method onCreateActionMode()?

  2. In MainActivity(), was it equally good to simply use clearChecks(); instead of the following in onDestroyActionMode():

     visitChecks(new ChoiceMode.Visitor() {
               @Override
               public void onCheckedPosition(int position) {
                 onChecked(position, false);
                 notifyItemChanged(position);
               }
             });
    
  3. I was trying to find out how actionMode is maintained across orientation changes in this sample app, because I was unable to find a call to startActionMode() in onRestoreInstanceState(). Eventually I found out that, in MainActivity, when we call adapter.onRestoreInstanceState(state); in onRestoreInstanceState(), we are supplying the saved ParcelableSparseBoolenArray checkStates (defined in class MultiChoiceMode) to the adapter which will later be used by the RowController in its bindModel() via cb.setChecked(adapter.isChecked(getAdapterPosition()));. This cb.setChecked() triggers onCheckedChanged() which calls adapter.onChecked(getAdapterPosition(), isChecked); where startActionMode() is called when first checked item is loaded on screen. Phew!
    But here a question comes to mind: whenever adapter.onChecked(getAdapterPosition(), isChecked); (in this case, via onCheckedChanged()), we are eventually calling overridden method setChecked() of MultiChoiceMode which is either saving the checked position in or deleting the unchecked position from the same ParcelableSparseBoolenArray checkStates. While it is required in case the user is actually checking or unchecking the list items but becomes a mere duplication (putting the values in checkStates that it already has) in case checked items are loaded to restore ActionMode in case of orientation change OR when user simply scrolls the list up and down and bindModel() method is recalled.
    Is above mentioned understanding regarding duplication in putting/deleting checked and unchecked positions from checkedStates, correct and is there a better approach for achieving the same objective?


#2

First, please understand that I wrote this code ~3 years ago, with minor bug fixes since then. Also, please note that Google is finally offering their own RecyclerView selection library, which you may wish to examine.

  1. You are correct that this is probably a duplicate assignment.

  2. I seem to recall that I used clearChecks() originally and changed it to this in response to a bug report.

  3. This did not seem to contain a question, and so I do not know what to answer. My apologies if I misunderstood what you asked. Again, now that Google has a selection library, please consider it.


#3

While I’ll look into android’s library, can u pl look at the third question once again. I’ve added a question in the end this time. Just need a confirmation from your side of my understanding and also a suggestion for a better approach.
Thanks for your time.


#4

Also, can u pl post a link to android’s selection library.


#5

Found it…that’s part of AndroidX. Will wait till it’s incorporated in your book.


#6

Is above mentioned understanding regarding duplication in putting/deleting checked and unchecked positions from checkedStates

Ideally, there should be no changes to the states on mere scrolling, and configuration changes should replace the entire set of checked states.

is there a better approach for achieving the same objective?

I have no idea, sorry.