+ @Override
+ public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
+ // Run the default commands.
+ super.onSaveInstanceState(savedInstanceState);
+
+ // Get the array of the checked items.
+ SparseBooleanArray checkedBookmarksSparseBooleanArray = bookmarksListView.getCheckedItemPositions();
+
+ // Create a checked items array list.
+ ArrayList<Integer> checkedBookmarksArrayList = new ArrayList<>();
+
+ // Add each checked bookmark position to the array list.
+ for (int i = 0; i < checkedBookmarksSparseBooleanArray.size(); i++) {
+ // Check to see if the bookmark is currently checked. Bookmarks that have previously been checked but currently aren't will be populated in the sparse boolean array, but will return false.
+ if (checkedBookmarksSparseBooleanArray.valueAt(i)) {
+ // Add the bookmark position to the checked bookmarks array list.
+ checkedBookmarksArrayList.add(checkedBookmarksSparseBooleanArray.keyAt(i));
+ }
+ }
+
+ // Store the checked items array list in the saved instance state.
+ savedInstanceState.putIntegerArrayList(CHECKED_BOOKMARKS_ARRAY_LIST, checkedBookmarksArrayList);
+ }
+