Skip to content

Multi select rotation bug (#899) #1411

Closed

Description

@AndyScherzinger

This issue belongs to #899 and probably is a missing onSaveInstanceState implementation

Scenario:

  • long press a single list item --> multi selection appears with menu items in the action bar:
    device-2016-01-07-201059
  • rotate screen --> menu and title are gone
    device-2016-01-07-201121
  • select a second list item --> menu is back
    device-2016-01-07-201136
  • rotate the screen again --> menu and title are gone
    device-2016-01-07-201154

Activity

  1. AndyScherzinger commented on Mar 13, 2016

    @AndyScherzinger
    ContributorAuthor

    @tobiasKaminsky Hah, i already opened an issue for this a while ago 😄

  2. tobiasKaminsky commented on Mar 14, 2016

    @tobiasKaminsky
    Contributor

    emilsjolander/StickyListHeaders#255
    There seems to be a solution, but I cannot get it to work :/

  3. AndyScherzinger commented on Mar 14, 2016

    @AndyScherzinger
    ContributorAuthor

    Hmm, I might be able to also have a look, but might not find the time before the weekend to give it a try but would also have thought that onSaveInstanceState/onRestoreInstanceState might be the way to deal with it. The CAB is still there it "just" looses all menu items. funny thing is that when you select further list items after the rotation the menu appears again. So maybe there is a listener that need to be called / event to be thrown. Tricky one :(

  4. DavidWiesner commented on Jun 1, 2016

    @DavidWiesner

    The problem could be that the getCheckedItemCount() in updateActionsMenu() return 0 after rotating the device. Thats happen when the ChoiceMode is modified in code (see: http://stackoverflow.com/a/24301175)

    One solution could be remove getCheckedItemCount() call and use the Adapters getCheckedItems().size() count.

  5. tobiasKaminsky commented on Jun 2, 2016

    @tobiasKaminsky
    Contributor

    @DavidWiesner I tried it, but now all selected items get removed. Before the change only the action menu vanished...

  6. DavidWiesner commented on Jun 3, 2016

    @DavidWiesner

    @tobiasKaminsky do you store any information about the selected items e.g.: in onSaveInstanceState()?
    Todo so add two methods to the FileListListAdapter

        private static final String SELECTION_KEY = "multiFileSelectionsKey";
    
        public void restoreSelectionState(Bundle savedInstanceState){
            if (savedInstanceState == null) {
                return;
            }
            long[] selectionState = savedInstanceState.getLongArray(SELECTION_KEY);
            mSelection.clear();
            if(selectionState != null){
                for (long id : selectionState) {
                    mSelection.put(id, true);
                }
            }
        }
    
        public void saveSelectionState(Bundle outState) {
            ArrayList<Integer> selectionState = new ArrayList<Integer>();
            for (Map.Entry<Integer, Boolean> entry : mSelection.entrySet()){
                if (entry.getValue()){
                    selectionState.add(entry.getKey());
                }
            }
            // convert to promitive long array
            long[] selectionStatePrimitive = new long[selectionState.size()];
            int i = 0;
            for (Long id : selectionState) {
                selectionStatePrimitive[i++] = id.longValue();
            }
            outState.putLongArray(SELECTION_KEY, selectionStatePrimitive);
        }

    Then call mAdapter.saveSelectionState(outState) in OCFileListFragment::onSaveInstanceState and mAdapter.restoreSelectionState(savedInstanceState) in onActivityCreated.

    Your solution has some another possible issues. On rotation the list of files will be synced again and so there is a chance new items will be added and the position is of selected items will change. So because your the adapter has stable Ids you can save this Ids to the mSelection HastMap. Then you have to adapt getCheckedItemPositions and getCheckedItems. getCheckedItemPositions can be remove because you never use this. getCheckedItems has than to iterate over all files and check if there id is in the mSelection HastMap. And adapt getView for check the id is in mSelection. not the position.
    Another improvement is to replace the HashMap with a HashSet, because you never check for the boolean value to be false.

    So you will have something like this:

        private HashSet<Long> mSelection = new LinkedHashSet<Long>();
    
        private static final String SELECTION_KEY = "multiFileSelectionsKey";
    
        public void restoreSelectionState(Bundle savedInstanceState){
            if (savedInstanceState == null) {
                return;
            }
            long[] selectionState = savedInstanceState.getLongArray(SELECTION_KEY);
            mSelection.clear();
            if(selectionState != null){
                for (long id : selectionState) {
                    mSelection.add(id);
                }
            }
        }
    
        public void saveSelectionState(Bundle outState) {
            long[] selectionStatePrimitive = new long[mSelection.size()];
            int i = 0;
            for (Long id : mSelection) {
                selectionStatePrimitive[i++] = id;
            }
            outState.putLongArray(SELECTION_KEY, selectionStatePrimitive);
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // ...
            if (mSelection.contains(getItemId(position))) {
                view.setBackgroundColor(mContext.getResources().getColor(R.color.selected_item_background));
            } else {
                view.setBackgroundColor(Color.WHITE);
            }
    
            return view;
        }
    
        public void setNewSelection(long id, boolean checked) {
            if(checked){
                mSelection.add(id);
            } else {
                mSelection.remove(id);
            }
            notifyDataSetChanged();
        }
    
        public void removeSelection(long id) {
            mSelection.remove(id);
            notifyDataSetChanged();
        }
    
        public void removeSelection(){
            mSelection.clear();
            notifyDataSetChanged();
        }
    
        public ArrayList<OCFile> getCheckedItems() {
            ArrayList<OCFile> files = new ArrayList<OCFile>();
            for(OCFile file: mFiles){
                if(mSelection.contains(file.getFileId())){
                    files.add(file);
                }
            }
            return files;
        }

    Then you have to change also all calls to removeSelection and setNewSelection to pass the id instead of the position e.g.: OCFileListFragment.java#L352

  7. tobiasKaminsky commented on Jun 3, 2016

    @tobiasKaminsky
    Contributor

    Many thanks for your help!
    As I am currently busy right now, maybe you want to try this by yourself and if it works create a patch or create a PR from your repository to this branch?

  8. davivel commented on Jun 13, 2016

    @davivel
    Contributor

    In a fast test, this seems fixed now in #1176; will mark it as ready to test so that QA have a real look.

  9. DavidWiesner commented on Jun 14, 2016

    @DavidWiesner

    @tobiasKaminsky my solution wasn't rigth. The main reason for this problem is that the ActionMenu wasn't updated after rotation after the directory was set. Here is a PR #1714

  10. jesmrec commented on Jun 28, 2016

    @jesmrec
    Member

    Fixed!

  11. davivel commented on Jul 5, 2016

    @davivel
    Contributor

    Great :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions