+ // Store the domains settings in the arguments bundle.
+ argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseIdToDelete);
+ argumentsBundle.putInt(DomainSettingsFragment.SCROLL_Y, domainSettingsScrollY);
+
+ // Instantiate a new domain settings fragment.
+ DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
+
+ // Add the arguments bundle to the domain settings fragment.
+ domainSettingsFragment.setArguments(argumentsBundle);
+
+ // Display the correct fragments.
+ if (twoPanedMode) { // The device in in two-paned mode.
+ // Get a cursor with the current contents of the domains database.
+ Cursor undoDeleteDomainsCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomain();
+
+ // Setup the domains cursor adapter.
+ CursorAdapter undoDeleteDomainsCursorAdapter = new CursorAdapter(getApplicationContext(), undoDeleteDomainsCursor, false) {
+ @Override
+ public View newView(Context context, Cursor cursor, ViewGroup parent) {
+ // Inflate the individual item layout.
+ return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
+ }
+
+ @Override
+ public void bindView(View view, Context context, Cursor cursor) {
+ /// Get the domain name string.
+ String domainNameString = cursor.getString(cursor.getColumnIndexOrThrow(DomainsDatabaseHelper.DOMAIN_NAME));
+
+ // Get a handle for the domain name text view.
+ TextView domainNameTextView = view.findViewById(R.id.domain_name_textview);
+
+ // Display the domain name.
+ domainNameTextView.setText(domainNameString);
+ }
+ };
+
+ // Update the domains list view.
+ domainsListView.setAdapter(undoDeleteDomainsCursorAdapter);
+
+ // Select the previously deleted domain in the list view.
+ domainsListView.setItemChecked(deletedDomainPosition, true);
+
+ // Display the domain settings fragment.
+ fragmentManager.beginTransaction().replace(R.id.domain_settings_fragment_container, domainSettingsFragment).commit();
+
+ // Enable the options delete menu item.
+ deleteMenuItem.setEnabled(true);
+
+ // Set the delete menu item icon.
+ deleteMenuItem.setIcon(R.drawable.delete_enabled);
+ } else { // The device in in one-paned mode.
+ // Display the domain settings fragment.
+ fragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
+
+ // Hide the add domain floating action button.
+ addDomainFAB.hide();
+
+ // Show and enable the delete menu item.
+ deleteMenuItem.setVisible(true);
+
+ // Display the domain settings fragment.
+ fragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
+ }
+ } else { // The snackbar was dismissed without the undo button being pushed.
+ // Delete the selected domain.
+ domainsDatabaseHelper.deleteDomain(databaseIdToDelete);
+
+ // Enable the delete menu item if the system was waiting for a snackbar to be dismissed.
+ if (dismissingSnackbar) {
+ // Create a `Runnable` to enable the delete menu item.
+ Runnable enableDeleteMenuItemRunnable = () -> {
+ // Enable the delete menu item according to the display mode.
+ if (twoPanedMode) { // Two-paned mode.
+ // Enable the delete menu item.
+ deleteMenuItem.setEnabled(true);
+
+ // Set the delete menu item icon.
+ deleteMenuItem.setIcon(R.drawable.delete_enabled);
+ } else { // Single-paned mode.
+ // Show the delete menu item.
+ deleteMenuItem.setVisible(true);
+ }
+
+ // Reset the dismissing snackbar tracker.
+ dismissingSnackbar = false;
+ };
+
+ // Enable the delete menu icon after 100 milliseconds to make sure that the previous domain has been deleted from the database.
+ Handler handler = new Handler();
+ handler.postDelayed(enableDeleteMenuItemRunnable, 100);
+ }
+
+ // Close the activity if back was pressed.
+ if (closeActivityAfterDismissingSnackbar) {
+ // Go home.
+ NavUtils.navigateUpFromSameTask(activity);
+ }
+ }
+ }
+ });
+
+ // Show the Snackbar.
+ undoDeleteSnackbar.show();
+ }
+ }
+
+ // Consume the event.
+ return true;
+ }
+
+ @Override
+ protected void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
+ // Run the default commands.
+ super.onSaveInstanceState(savedInstanceState);
+
+ // Get a handle for the domain settings scrollview.
+ ScrollView domainSettingsScrollView = findViewById(R.id.domain_settings_scrollview);
+
+ // Check to see if the domain settings scrollview exists.
+ if (domainSettingsScrollView == null) { // The domain settings are not displayed.
+ // Store the domain settings status in the bundle.
+ savedInstanceState.putBoolean(DOMAIN_SETTINGS_DISPLAYED, false);
+ savedInstanceState.putInt(DOMAIN_SETTINGS_DATABASE_ID, -1);
+ savedInstanceState.putInt(DOMAIN_SETTINGS_SCROLL_Y, 0);
+ } else { // The domain settings are displayed.
+ // Save any changes that have been made to the domain settings.
+ saveDomainSettings(coordinatorLayout, resources);
+
+ // Get the domain settings scroll Y.
+ int domainSettingsScrollY = domainSettingsScrollView.getScrollY();
+
+ // Store the domain settings status in the bundle.
+ savedInstanceState.putBoolean(DOMAIN_SETTINGS_DISPLAYED, true);
+ savedInstanceState.putInt(DOMAIN_SETTINGS_DATABASE_ID, DomainSettingsFragment.databaseId);
+ savedInstanceState.putInt(DOMAIN_SETTINGS_SCROLL_Y, domainSettingsScrollY);
+ }
+
+ // Check to see if the domains listview exists.
+ if (domainsListView != null) {
+ // Get the domains listview position.
+ int domainsListViewPosition = domainsListView.getFirstVisiblePosition();
+
+ // Store the listview position in the bundle.
+ savedInstanceState.putInt(LISTVIEW_POSITION, domainsListViewPosition);
+ }
+ }
+
+ // Control what the navigation bar back button does.
+ @Override
+ public void onBackPressed() {
+ // Get a handle for the fragment manager.
+ FragmentManager fragmentManager = getSupportFragmentManager();
+
+ if (twoPanedMode) { // The device is in two-paned mode.
+ // Save the current domain settings if the domain settings fragment is displayed.
+ if (findViewById(R.id.domain_settings_scrollview) != null) {
+ saveDomainSettings(coordinatorLayout, resources);
+ }
+
+ // Dismiss the undo delete SnackBar if it is shown.
+ if ((undoDeleteSnackbar != null) && undoDeleteSnackbar.isShown()) {
+ // Set the close flag.
+ closeActivityAfterDismissingSnackbar = true;
+
+ // Dismiss the snackbar.
+ undoDeleteSnackbar.dismiss();
+ } else {
+ // Pass `onBackPressed()` to the system.
+ super.onBackPressed();
+ }
+ } else if (closeOnBack) { // Go directly back to the main WebView activity because the domains activity was launched from the options menu.
+ // Save the current domain settings.
+ saveDomainSettings(coordinatorLayout, resources);
+
+ // Pass `onBackPressed()` to the system.
+ super.onBackPressed();
+ } else if (findViewById(R.id.domain_settings_scrollview) != null) { // The device is in single-paned mode and domain settings fragment is displayed.
+ // Save the current domain settings.
+ saveDomainSettings(coordinatorLayout, resources);
+
+ // Display the domains list fragment.
+ DomainsListFragment domainsListFragment = new DomainsListFragment();
+ fragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
+ fragmentManager.executePendingTransactions();
+
+ // Populate the list of domains. `-1` highlights the first domain if in two-paned mode. It has no effect in single-paned mode.
+ populateDomainsListView(-1, domainsListViewPosition);
+
+ // Show the add domain floating action button.
+ addDomainFAB.show();
+
+ // Hide the delete menu item.
+ deleteMenuItem.setVisible(false);
+ } else { // The device is in single-paned mode and the domain list fragment is displayed.
+ // Dismiss the undo delete SnackBar if it is shown.
+ if ((undoDeleteSnackbar != null) && undoDeleteSnackbar.isShown()) {
+ // Set the close flag.
+ closeActivityAfterDismissingSnackbar = true;
+
+ // Dismiss the snackbar.
+ undoDeleteSnackbar.dismiss();
+ } else {
+ // Pass `onBackPressed()` to the system.
+ super.onBackPressed();
+ }
+ }