+ @Override
+ protected void onSaveInstanceState(Bundle outState) {
+ // Store the current `DomainSettingsFragment` state in `outState`.
+ if (findViewById(R.id.domain_settings_scrollview) != null) { // `DomainSettingsFragment` is displayed.
+ // Save any changes that have been made to the domain settings.
+ saveDomainSettings();
+
+ // Store `DomainSettingsDisplayed`.
+ outState.putBoolean("domainSettingsDisplayed", true);
+ outState.putInt("domainSettingsDatabaseId", DomainSettingsFragment.databaseId);
+ } else { // `DomainSettingsFragment` is not displayed.
+ outState.putBoolean("domainSettingsDisplayed", false);
+ outState.putInt("domainSettingsDatabaseId", -1);
+ }
+
+ super.onSaveInstanceState(outState);
+ }
+
+ // Control what the navigation bar back button does.
+ @Override
+ public void onBackPressed() {
+ 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();
+ }
+
+ // Dismiss the undo delete SnackBar if it is shown.
+ if (undoDeleteSnackbar != null && undoDeleteSnackbar.isShown()) {
+ undoDeleteSnackbar.dismiss();
+
+ // Create a runnable to return to the main activity.
+ Runnable navigateHomeRunnable = super::onBackPressed;
+
+ // Navigate home after 300 milliseconds to make sure that the previous domain has been deleted from the database.
+ Handler handler = new Handler();
+ handler.postDelayed(navigateHomeRunnable, 300);
+ } 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();
+
+ // 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();
+
+ // Display the domains list fragment.
+ DomainsListFragment domainsListFragment = new DomainsListFragment();
+ supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
+ supportFragmentManager.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);
+
+ // Display the add domain FAB.
+ addDomainFAB.setVisibility(View.VISIBLE);
+
+ // 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()) {
+ undoDeleteSnackbar.dismiss();
+
+ // Create a runnable to return to the main activity.
+ Runnable navigateHomeRunnable = super::onBackPressed;
+
+ // Navigate home after 300 milliseconds to make sure that the previous domain has been deleted from the database.
+ Handler handler = new Handler();
+ handler.postDelayed(navigateHomeRunnable, 300);
+ } else {
+ // Pass `onBackPressed()` to the system.
+ super.onBackPressed();
+ }
+ }
+ }
+