Skip to content
This repository was archived by the owner on Apr 9, 2021. It is now read-only.

Commit 09cb7c8

Browse files
committed
Fixes On screen rotation selected forms get unselected bug
Using onSaveInstanceState() and restoring in the onCreateView() Removing ViewModel Fixes On screen rotation selected forms get unselected bug Using onSaveInstanceState() and restoring in the onCreateView()
1 parent bf4ce1f commit 09cb7c8

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

share_app/src/main/java/org/odk/share/fragments/BlankFormsFragment.java

+18
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public class BlankFormsFragment extends FormListFragment implements LoaderManage
5353
private static final String FORM_CHOOSER_LIST_SORTING_ORDER = "formChooserListSortingOrder";
5454

5555
public static final String FORM_IDS = "form_ids";
56+
private final String SELECTED_INSTANCES_KEY="ROTATION_SELECTED_INSTANCES";
5657

5758
private static final int FORM_LOADER = 2;
5859
private FormsAdapter formAdapter;
@@ -74,6 +75,16 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
7475
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
7576
llm.setOrientation(LinearLayoutManager.VERTICAL);
7677
recyclerView.setLayoutManager(llm);
78+
79+
if(savedInstanceState!=null)
80+
{
81+
long[] previousSelectedInstances=savedInstanceState.getLongArray(SELECTED_INSTANCES_KEY);
82+
for(int i=0;i<previousSelectedInstances.length;i++)
83+
{
84+
selectedForms.add(previousSelectedInstances[i]);
85+
}
86+
}
87+
7788
return view;
7889
}
7990

@@ -89,6 +100,13 @@ public Loader<Cursor> onCreateLoader(int id, Bundle args) {
89100
return new FormsDao().getFormsCursorLoader(getFilterText(), getSortingOrder());
90101
}
91102

103+
@Override public void onSaveInstanceState(@NonNull Bundle outState) {
104+
super.onSaveInstanceState(outState);
105+
outState.putLongArray(SELECTED_INSTANCES_KEY, ArrayUtils.toPrimitive(
106+
selectedForms.toArray(new Long[selectedInstances.size()])));
107+
108+
}
109+
92110
@Override
93111
public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor cursor) {
94112
if (cursor != null) {

share_app/src/main/java/org/odk/share/fragments/FilledFormsFragment.java

+20-3
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,34 @@ public class FilledFormsFragment extends InstanceListFragment implements LoaderM
5656
private static final int INSTANCE_LOADER = 1;
5757
private InstanceAdapter instanceAdapter;
5858
private LinkedHashSet<Long> selectedInstances;
59+
private final String SELECTED_INSTANCES_KEY="ROTATION_SELECTED_INSTANCES";
5960

6061

6162
public FilledFormsFragment() {
6263
}
6364

6465
@Override
6566
public View onCreateView(LayoutInflater inflater, ViewGroup container,
66-
Bundle savedInstanceState) {
67+
Bundle savedInstanceState) {
6768
setHasOptionsMenu(true);
6869
View view = inflater.inflate(R.layout.fragment_forms, container, false);
6970
ButterKnife.bind(this, view);
70-
7171
selectedInstances = new LinkedHashSet<>();
72-
7372
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
7473
llm.setOrientation(LinearLayoutManager.VERTICAL);
7574
recyclerView.setLayoutManager(llm);
75+
76+
if(savedInstanceState!=null)
77+
{
78+
long[] previousSelectedInstances=savedInstanceState.getLongArray(SELECTED_INSTANCES_KEY);
79+
for(int i=0;i<previousSelectedInstances.length;i++)
80+
{
81+
selectedInstances.add(previousSelectedInstances[i]);
82+
}
83+
}
84+
7685
return view;
86+
7787
}
7888

7989
@Override
@@ -88,6 +98,13 @@ public Loader<Cursor> onCreateLoader(int id, Bundle args) {
8898
return new InstancesDao().getSavedInstancesCursorLoader(getFilterText(), getSortingOrder());
8999
}
90100

101+
@Override public void onSaveInstanceState(@NonNull Bundle outState) {
102+
super.onSaveInstanceState(outState);
103+
outState.putLongArray(SELECTED_INSTANCES_KEY, ArrayUtils.toPrimitive(
104+
selectedInstances.toArray(new Long[selectedInstances.size()])));
105+
106+
}
107+
91108
@Override
92109
public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor cursor) {
93110
if (cursor != null) {

0 commit comments

Comments
 (0)