From 5545ec452e70a3b8a99ff7b4bef330c1333d1a60 Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Wed, 19 Jul 2017 17:04:17 -0700 Subject: [PATCH] Reload `DomainSettingsFragment` on rotate. Implements https://redmine.stoutner.com/issues/172. --- .../activities/DomainsActivity.java | 121 +++++++++++++----- .../activities/MainWebViewActivity.java | 1 - .../fragments/DomainSettingsFragment.java | 4 +- .../fragments/DomainsListFragment.java | 3 - 4 files changed, 89 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/DomainsActivity.java b/app/src/main/java/com/stoutner/privacybrowser/activities/DomainsActivity.java index 59c5fb9f..2a2b92db 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/DomainsActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/DomainsActivity.java @@ -49,15 +49,12 @@ import com.stoutner.privacybrowser.fragments.DomainsListFragment; import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper; public class DomainsActivity extends AppCompatActivity implements AddDomainDialog.AddDomainListener { - // `twoPanedMode` is public static so it can be accessed from `DomainsListFragment`. It is also used in `onCreate()` and `populateDomainsListView()`. + // `twoPanedMode` is public static so it can be accessed from `DomainsListFragment`. It is also used in `onCreate()`, `onCreateOptionsMenu()`, and `populateDomainsListView()`. public static boolean twoPanedMode; - // `databaseId` is public static so it can be accessed from `DomainsListFragment`. It is also used in `saveDomainSettings()` and `populateDomainsListView()`. + // `databaseId` is public static so it can be accessed from `DomainsListFragment`. It is also used in `onCreateOptionsMenu()`, `saveDomainSettings()` and `populateDomainsListView()`. public static int currentDomainDatabaseId; - // `domainSettingsFragmentDisplayed` is public static so it can be accessed from `DomainsListFragment`. It is also used in `onCreate()`, `onOptionsItemSelected()`, and `onBackPressed()`. - public static boolean domainSettingsFragmentDisplayed; - // `deleteMenuItem` is public static so it can be accessed from `DomainsListFragment`. It is also used in `onCreateOptionsMenu()`, `onOptionsItemSelected()`, and `onBackPressed()`. public static MenuItem deleteMenuItem; @@ -70,7 +67,7 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo // `context` is used in `onCreate()`, `onOptionsItemSelected()`, and `onAddDomain()`. private Context context; - // `supportFragmentManager` is used in `onCreate()`. + // `supportFragmentManager` is used in `onCreate()` and `onCreateOptionsMenu()`. private FragmentManager supportFragmentManager; // `domainsDatabaseHelper` is used in `onCreate()` and `saveDomainSettings()`. @@ -79,9 +76,21 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo // `domainsListView` is used in `onCreate()` and `populateDomainsList()`. private ListView domainsListView; - // `addDomainFAB` is used in `onCreate()`, `onOptionsItemSelected()`, and `onBackPressed()`. + // `addDomainFAB` is used in `onCreate()`, `onCreateOptionsMenu()`, `onOptionsItemSelected()`, and `onBackPressed()`. private FloatingActionButton addDomainFAB; + // `deletedDomainPosition` is used in an inner and outer class in `onOptionsItemSelected()`. + private int deletedDomainPosition; + + // `restartAfterRotate` is used in `onCreate()` and `onCreateOptionsMenu()`. + private boolean restartAfterRotate; + + // `domainSettingsDisplayedBeforeRotate` is used in `onCreate()` and `onCreateOptionsMenu()`. + private boolean domainSettingsDisplayedBeforeRotate; + + // `domainSettingsDatabaseIdBeforeRotate` is used in `onCreate()` and `onCreateOptionsMenu()`. + private int domainSettingsDatabaseIdBeforeRotate; + @Override protected void onCreate(Bundle savedInstanceState) { // Set the activity theme. @@ -94,6 +103,13 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo // Run the default commands. super.onCreate(savedInstanceState); + // Extract the values from `savedInstanceState` if it is not `null`. + if (savedInstanceState != null) { + restartAfterRotate = true; + domainSettingsDisplayedBeforeRotate = savedInstanceState.getBoolean("domainSettingsDisplayed"); + domainSettingsDatabaseIdBeforeRotate = savedInstanceState.getInt("domainSettingsDatabaseId"); + } + // Set the content view. setContentView(R.layout.domains_coordinatorlayout); @@ -118,11 +134,6 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo // Determine if we are in two pane mode. `domain_settings_fragment_container` does not exist on devices with a width less than 900dp. twoPanedMode = (findViewById(R.id.domain_settings_fragment_container) != null); - // Display `DomainsListFragment`. - DomainsListFragment domainsListFragment = new DomainsListFragment(); - supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit(); - supportFragmentManager.executePendingTransactions(); - // Configure `addDomainFAB`. addDomainFAB = (FloatingActionButton) findViewById(R.id.add_domain_fab); addDomainFAB.setOnClickListener(new View.OnClickListener() { @@ -146,8 +157,50 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo // Only display `deleteMenuItem` (initially) in two-paned mode. deleteMenuItem.setVisible(twoPanedMode); - // Populate the list of domains. We have to do this from `onCreateOptionsMenu()` instead of `onCreate()` because `populateDomainsListView()` needs the `deleteMenuItem` to be inflated. `-1` highlights the first domain. - populateDomainsListView(-1); + // Display the fragments. This must be done from `onCreateOptionsMenu()` instead of `onCreate()` because `populateDomainsListView()` needs `deleteMenuItem` to be inflated. + if (restartAfterRotate && !twoPanedMode && domainSettingsDisplayedBeforeRotate) { // The device was rotated, the new configuration is in single-paned mode, and domain settings were displayed previously. + // Reset `restartAfterRotate`. + restartAfterRotate = false; + + // Store `currentDomainDatabaseId`. + currentDomainDatabaseId = domainSettingsDatabaseIdBeforeRotate; + + // Add `currentDomainDatabaseId` to `argumentsBundle`. + Bundle argumentsBundle = new Bundle(); + argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, currentDomainDatabaseId); + + // Add `argumentsBundle` to `domainSettingsFragment`. + DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment(); + domainSettingsFragment.setArguments(argumentsBundle); + + // Show `deleteMenuItem`. + deleteMenuItem.setVisible(true); + + // Hide `add_domain_fab`. + addDomainFAB.setVisibility(View.GONE); + + // Display `domainSettingsFragment`. + supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit(); + } else if (restartAfterRotate && twoPanedMode && domainSettingsDisplayedBeforeRotate) { // The device was rotated, the new configuration is in two-paned mode, and domain settings were displayed previously. + // Reset `restartAfterRotate`. + restartAfterRotate = false; + + // Display `DomainsListFragment`. + DomainsListFragment domainsListFragment = new DomainsListFragment(); + supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit(); + supportFragmentManager.executePendingTransactions(); + + // Populate the list of domains. `domainSettingsDatabaseId` highlights the domain that was highlighted before the rotation. + populateDomainsListView(domainSettingsDatabaseIdBeforeRotate); + } else { // The device was not rotated or, if it was, domain settings were not displayed previously. + // Display `DomainsListFragment`. + 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. + populateDomainsListView(-1); + } // Success! return true; @@ -168,7 +221,7 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo // Go home. NavUtils.navigateUpFromSameTask(this); - } else if (domainSettingsFragmentDisplayed) { // The device is in single-paned mode and `DomainSettingsFragment` is displayed. + } else if (findViewById(R.id.domain_settings_scrollview) != null) { // The device is in single-paned mode and `DomainSettingsFragment` is displayed. // Save the current domain settings. saveDomainSettings(); @@ -180,9 +233,6 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo // 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); - // Update `domainSettingsFragmentDisplayed`. - domainSettingsFragmentDisplayed = false; - // Display `addDomainFAB`. addDomainFAB.setVisibility(View.VISIBLE); @@ -198,11 +248,11 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo // Store a copy of `currentDomainDatabaseId` because it could change while the `Snackbar` is displayed. final int databaseIdToDelete = currentDomainDatabaseId; - // Store the deleted domain position, which is needed if `Undo` is selected in the `Snackbar`. - final int deletedDomainPosition = domainsListView.getCheckedItemPosition(); - // Update the fragments and menu items. if (twoPanedMode) { // Two-paned mode. + // Store the deleted domain position, which is needed if `Undo` is selected in the `Snackbar`. + deletedDomainPosition = domainsListView.getCheckedItemPosition(); + // Disable the options `MenuItems`. deleteMenuItem.setEnabled(false); deleteMenuItem.setIcon(R.drawable.delete_blue); @@ -215,9 +265,6 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit(); supportFragmentManager.executePendingTransactions(); - // Update `domainSettingsFragmentDisplayed`. - domainSettingsFragmentDisplayed = false; - // Display `addDomainFAB`. addDomainFAB.setVisibility(View.VISIBLE); @@ -317,9 +364,6 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo // Show and enable `deleteMenuItem`. deleteMenuItem.setVisible(true); - // Set `domainSettingsFragmentDisplayed`. - domainSettingsFragmentDisplayed = true; - // Display `domainSettingsFragment`. supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit(); } @@ -373,6 +417,21 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo return true; } + @Override + protected void onSaveInstanceState(Bundle outState) { + // Store the current `DomainSettingsFragment` state in `outState`. + if (findViewById(R.id.domain_settings_scrollview) != null) { // `DomainSettingsFragment` is displayed. + // 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() { @@ -384,7 +443,7 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo // Go home. NavUtils.navigateUpFromSameTask(this); - } else if (domainSettingsFragmentDisplayed) { // The device is in single-paned mode and `DomainSettingsFragment` is displayed. + } else if (findViewById(R.id.domain_settings_scrollview) != null) { // The device is in single-paned mode and `DomainSettingsFragment` is displayed. // Save the current domain settings. saveDomainSettings(); @@ -396,9 +455,6 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo // 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); - // Update `domainSettingsFragmentDisplayed`. - domainSettingsFragmentDisplayed = false; - // Display `addDomainFAB`. addDomainFAB.setVisibility(View.VISIBLE); @@ -434,9 +490,6 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo // Show and enable `deleteMenuItem`. DomainsActivity.deleteMenuItem.setVisible(true); - // Set `domainSettingsFragmentDisplayed`. - DomainsActivity.domainSettingsFragmentDisplayed = true; - // Add `currentDomainDatabaseId` to `argumentsBundle`. Bundle argumentsBundle = new Bundle(); argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, currentDomainDatabaseId); diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java index f37f1241..6442fb2c 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java @@ -34,7 +34,6 @@ import android.content.SharedPreferences; import android.content.res.Configuration; import android.database.Cursor; import android.graphics.Bitmap; -import android.graphics.Typeface; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.net.Uri; diff --git a/app/src/main/java/com/stoutner/privacybrowser/fragments/DomainSettingsFragment.java b/app/src/main/java/com/stoutner/privacybrowser/fragments/DomainSettingsFragment.java index 98a7e929..b47dec32 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/fragments/DomainSettingsFragment.java +++ b/app/src/main/java/com/stoutner/privacybrowser/fragments/DomainSettingsFragment.java @@ -50,8 +50,8 @@ public class DomainSettingsFragment extends Fragment { // `DATABASE_ID` is used by activities calling this fragment. public static final String DATABASE_ID = "database_id"; - // `databaseId` is used in `onCreate()` and `onCreateView()`. - private int databaseId; + // `databaseId` is public statis so it can be accessed from `DomainsActivity`. It is also used in `onCreate()` and `onCreateView()`. + public static int databaseId; @Override public void onCreate(Bundle savedInstanceState) { diff --git a/app/src/main/java/com/stoutner/privacybrowser/fragments/DomainsListFragment.java b/app/src/main/java/com/stoutner/privacybrowser/fragments/DomainsListFragment.java index 36bc9b74..553c107d 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/fragments/DomainsListFragment.java +++ b/app/src/main/java/com/stoutner/privacybrowser/fragments/DomainsListFragment.java @@ -143,9 +143,6 @@ public class DomainsListFragment extends Fragment { FloatingActionButton addDomainFAB = (FloatingActionButton) getActivity().findViewById(R.id.add_domain_fab); addDomainFAB.setVisibility(View.GONE); - // Set `domainSettingsFragmentDisplayed`. - DomainsActivity.domainSettingsFragmentDisplayed = true; - // Display `domainSettingsFragment`. supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit(); } -- 2.45.2