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

Commit

Permalink
Fixes On screen rotation selected forms get unselected bug
Browse files Browse the repository at this point in the history
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()
  • Loading branch information
iadeelzafar committed Mar 9, 2019
1 parent f0b381d commit 27231a9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class BlankFormsFragment extends FormListFragment implements LoaderManage

public static final String FORM_IDS = "form_ids";
private static final String FORM_CHOOSER_LIST_SORTING_ORDER = "formChooserListSortingOrder";
private static final String SELECTED_INSTANCES_KEY = "ROTATION_SELECTED_INSTANCES";
private static final int FORM_LOADER = 2;

@BindView(R.id.recyclerview)
Expand Down Expand Up @@ -88,6 +89,15 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(llm);

if (savedInstanceState != null) {
long[] previousSelectedInstances = savedInstanceState.getLongArray(SELECTED_INSTANCES_KEY);
for (long previousSelectedInstance : previousSelectedInstances) {
selectedForms.add(previousSelectedInstance);
}
sendButton.setEnabled(selectedForms.size() > 0);
}

return view;
}

Expand All @@ -103,6 +113,14 @@ public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return formsDao.getFormsCursorLoader(getFilterText(), getSortingOrder());
}

@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putLongArray(SELECTED_INSTANCES_KEY, ArrayUtils.toPrimitive(
selectedForms.toArray(new Long[selectedForms.size()])));

}

@Override
public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor cursor) {
if (cursor != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class FilledFormsFragment extends InstanceListFragment implements LoaderM

private InstanceAdapter instanceAdapter;
private LinkedHashSet<Long> selectedInstances;
private static final String SELECTED_INSTANCES_KEY = "ROTATION_SELECTED_INSTANCES";

public FilledFormsFragment() {
}
Expand All @@ -71,12 +72,19 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
setHasOptionsMenu(true);
View view = inflater.inflate(R.layout.fragment_forms, container, false);
ButterKnife.bind(this, view);

selectedInstances = new LinkedHashSet<>();

LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(llm);

if (savedInstanceState != null) {
long[] previousSelectedInstances = savedInstanceState.getLongArray(SELECTED_INSTANCES_KEY);
for (long previousSelectedInstance : previousSelectedInstances) {
selectedInstances.add(previousSelectedInstance);
}
sendButton.setEnabled(selectedInstances.size() > 0);
}

return view;
}

Expand All @@ -92,6 +100,14 @@ public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return instancesDao.getSavedInstancesCursorLoader(getFilterText(), getSortingOrder());
}

@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putLongArray(SELECTED_INSTANCES_KEY, ArrayUtils.toPrimitive(
selectedInstances.toArray(new Long[selectedInstances.size()])));

}

@Override
public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor cursor) {
if (cursor != null) {
Expand Down

0 comments on commit 27231a9

Please sign in to comment.