From 1b10e75168043a7838629d48e3658c918d7caeca Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Sun, 29 Jul 2018 04:50:16 +0200 Subject: [PATCH] TunnelListFragment: fix multiselection on rotation Signed-off-by: Jason A. Donenfeld --- .../android/fragment/TunnelListFragment.java | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/wireguard/android/fragment/TunnelListFragment.java b/app/src/main/java/com/wireguard/android/fragment/TunnelListFragment.java index 53c6d612..3ecc2e7a 100644 --- a/app/src/main/java/com/wireguard/android/fragment/TunnelListFragment.java +++ b/app/src/main/java/com/wireguard/android/fragment/TunnelListFragment.java @@ -304,6 +304,26 @@ public class TunnelListFragment extends BaseFragment { Snackbar.make(binding.mainContainer, message, Snackbar.LENGTH_LONG).show(); } + @Override + public void onSaveInstanceState(final Bundle outState) { + super.onSaveInstanceState(outState); + + outState.putIntegerArrayList("CHECKED_ITEMS", actionModeListener.getCheckedItems()); + } + + @Override + public void onActivityCreated(@Nullable final Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + + if (savedInstanceState != null) { + final Collection checkedItems = savedInstanceState.getIntegerArrayList("CHECKED_ITEMS"); + if (checkedItems != null) { + for (final Integer i : checkedItems) + actionModeListener.setItemChecked(i, true); + } + } + } + @Override public void onViewStateRestored(@Nullable final Bundle savedInstanceState) { super.onViewStateRestored(savedInstanceState); @@ -393,6 +413,10 @@ public class TunnelListFragment extends BaseFragment { setItemChecked(position, !checkedItems.contains(position)); } + public ArrayList getCheckedItems() { + return new ArrayList<>(checkedItems); + } + void setItemChecked(final int position, final boolean checked) { if (checked) { checkedItems.add(position); @@ -406,7 +430,7 @@ public class TunnelListFragment extends BaseFragment { actionMode.finish(); } - if (binding != null) { + if (binding != null && binding.tunnelList.getAdapter() != null) { binding.tunnelList.getAdapter().notifyItemChanged(position); }