0

I've problem when using ListView in Fragment. When i using MultiChoiceModelListener, it will display ActionMode when i Long click on the item in ListView.

The problem is :

  1. When ActionMode is activated, and user replace that fragment with other fragment the actionmode still there, take a look the picture below.
  2. When I use ActionMode mode = null and instantiate that mode variable onCreateActionMode plus implement it in onDestroyActionMode and onDestroyView, please take a look the snippet code. The Problem number one is solved, but it's open another problem, because mode.finish() is called in onDestroyView when fragment orientation change my checked item in ListView disappear.
  3. When I delete onDestroyView() method, I solved the problem number two, but Problem number one is open again.

enter image description here

This is my snippet code :

//My ActionModeListener
private class MyActionMode implements AbsListView.MultiChoiceModeListener {

        @Override
        public void onItemCheckedStateChanged(ActionMode actionMode, int i, long l, boolean b) {
            String nfile = ((DataFileProvider) adapter.getItem(i)).getNfile();
            if (b) {
                selections.add(nfile);
            } else {
                selections.remove(nfile);
            }
            actionMode.setTitle(selections.size() + " " + getString(R.string.item_selected));
        }

        @Override
        public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
            mode = actionMode;
            getActivity().getMenuInflater().inflate(R.menu.list_file_action_mode, menu);
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
            actionMode.setTitle(selections.size() + " " + getString(R.string.item_selected));
            return true;
        }

        @Override
        public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
            return true;
        }

        @Override
        public void onDestroyActionMode(ActionMode actionMode) {
            selections.clear();
            mode = null;
            showMessage("ListDocFileFragment : onDestroyActionMode");
        }
    }

This is my snippet code when handle fragment livecycle

//My OnResume,OnPause,and OnDestroyView in Fragment
@Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            default_location = getArguments().getString("default_location");
        }
        if (getActivity().getIntent().hasExtra("ActionMode")) {
            selections = getActivity().getIntent().getStringArrayListExtra("ActionMode");
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if(selections.isEmpty() && list_doc_file.getCheckedItemCount() > 0){
            for (int i = 0; i < adapter.getCount(); i++) {
                list_doc_file.setItemChecked(i, false);
            }
            selections.clear();
        }
        showMessage("ListDocFileFragment : onResume");
    }

    @Override
    public void onPause() {
        if (selections != null) {
            getActivity().getIntent().putExtra("ActionMode", selections);
        }
        showMessage("ListDocFileFragment : onPause");
        super.onPause();
    }

    @Override
    public void onDestroyView() {
        if(mode != null){
            mode.finish();
        }

        showMessage("ListDocFileFragment : onDestroyView");
        super.onDestroyView();
    }

My Question is how to solved three problem above, without open another problem like i did ?

I've searching for another solution in this stack, but i got nothing. When i implement their solution it's always open another problem. Any suggestion ?

Eko Junaidi Salam
  • 1,663
  • 1
  • 18
  • 26

0 Answers0