Shree
Shree

Reputation: 384

ListView- setChoiceMode with CHOICE_MODE_MULTIPLE_MODAL for different orientations

I am using a listview in my application where have implemented setChoiceMode with ListView.CHOICE_MODE_MULTIPLE_MODAL. When long press on the listview, items can be selected but after that, if the screen orientation is changed the action bar goes annoyingly.

My code looks like this

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    adapter = new ItemListAdapter();
    listView.setAdapter(adapter);
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

    listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
        @Override

        public void onItemCheckedStateChanged(ActionMode actionMode, int position, long l, boolean b) {
            final int checkedItemCount = listView.getCheckedItemCount();
            actionMode.setTitle(checkedItemCount + " Selected");
            /*get the selected items*/
        }

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

        @Override
        public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(final ActionMode actionMode, MenuItem menuItem) {
            switch (menuItem.getItemId()){
                case R.id.delete_item:
                    /*delete selected items*/
                    return true;
                default:
                    return false;
            }
        }

        @Override
        public void onDestroyActionMode(ActionMode actionMode) {
            adapter.removeSelection();
        }
    });
}

Image in portrait mode & landscape mode ,It shows selected items as 0 & when trying to delete the respective selected items will not get deleted.then if back button is pressed (on landscape mode which is implemented in my application activity) the action bar color will turn to white.

(Same happens when trying in other way select items in landscape mode and rotate to delete the same in portrait)

One solution is using below in the manifest

android:configChanges="orientation|screenSize"

If this is used the activity is not recreated on orientation change.As it has different layouts for portrait and landscape, activity recreation is must on orientation change.

I know the issue is because of the activity is recreated on orientation change that makes setMultiChoiceModeListener to called every time.

can someone help to tackle the issue? Thanks!

Upvotes: 0

Views: 476

Answers (1)

InziKhan
InziKhan

Reputation: 116

Use these methods to store and restore the values of your selected items.

@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putInt("count",checkedItemCount);
    super.onSaveInstanceState(outState);
}

then restore it onCreate method

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null) {
        //will return value of selecteditems
        int countedItems = savedInstanceState.getInt("count");

    }
}

Here's a link for more information:
How to use onSavedInstanceState example please

Upvotes: 0

Related Questions