TunnelListFragment: fix multiselection on rotation

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2018-07-29 04:50:16 +02:00
parent 07359e392c
commit 1b10e75168

View File

@ -304,6 +304,26 @@ public class TunnelListFragment extends BaseFragment {
Snackbar.make(binding.mainContainer, message, Snackbar.LENGTH_LONG).show(); 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<Integer> checkedItems = savedInstanceState.getIntegerArrayList("CHECKED_ITEMS");
if (checkedItems != null) {
for (final Integer i : checkedItems)
actionModeListener.setItemChecked(i, true);
}
}
}
@Override @Override
public void onViewStateRestored(@Nullable final Bundle savedInstanceState) { public void onViewStateRestored(@Nullable final Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState); super.onViewStateRestored(savedInstanceState);
@ -393,6 +413,10 @@ public class TunnelListFragment extends BaseFragment {
setItemChecked(position, !checkedItems.contains(position)); setItemChecked(position, !checkedItems.contains(position));
} }
public ArrayList<Integer> getCheckedItems() {
return new ArrayList<>(checkedItems);
}
void setItemChecked(final int position, final boolean checked) { void setItemChecked(final int position, final boolean checked) {
if (checked) { if (checked) {
checkedItems.add(position); checkedItems.add(position);
@ -406,7 +430,7 @@ public class TunnelListFragment extends BaseFragment {
actionMode.finish(); actionMode.finish();
} }
if (binding != null) { if (binding != null && binding.tunnelList.getAdapter() != null) {
binding.tunnelList.getAdapter().notifyItemChanged(position); binding.tunnelList.getAdapter().notifyItemChanged(position);
} }