0

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!

Stuti Kasliwal
  • 771
  • 9
  • 20
Shree
  • 354
  • 2
  • 21

1 Answers1

0

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

Pang
  • 9,564
  • 146
  • 81
  • 122
InziKhan
  • 116
  • 1
  • 8
  • I tried even this before, the problem is the count value will be restored but the selected items will not get deleted. Any other suggestion? – Shree Dec 18 '17 at 04:17
  • Can some one help? – Shree Dec 24 '17 at 17:12
  • It is in onActionItemClicked method and works fine. As i mentioned earlier it works fine if we are working on one mode landscape or portrait. If i try to select the item in portrait and then try to delete those items by rotating the screen to landscape or vice versa is not possible. Any way to deal with screen orientation for setMultiChoice mode? – Shree Dec 25 '17 at 04:32
  • Got the solution. I was using different layouts for portrait and landscape mode to have the proper item alignment on the layout. That was not letting me to use **configChanges**. Now i worked around those layout files and using **android:configChanges="orientation|screenSize"** to make sure the activity is not recreated on orientation change and is working like a charm. Thanks. – Shree Dec 25 '17 at 05:49