X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserAndroid.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Factivities%2FDomainsActivity.java;h=cca885652f1fb04eccddabafcb71805fb62d9468;hp=80c5d4a13e48fee60e83306ee8d180fbc98da33b;hb=df38abb63dacb2ec493d7906dbc4bc995152a467;hpb=430b2b303a01cad55c8a8c69faa536f4bb228cc3 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 80c5d4a1..cca88565 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/DomainsActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/DomainsActivity.java @@ -19,6 +19,7 @@ package com.stoutner.privacybrowser.activities; +import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.res.Resources; @@ -70,6 +71,9 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo // `dismissingSnackbar` is public static so it can be accessed from `DomainsListFragment`. It is also used in `onOptionsItemSelected()`. public static boolean dismissingSnackbar; + // `closeActivityAfterDismissingSnackbar` is used in `onOptionsItemSelected()`, and `onBackPressed()`. + private boolean closeActivityAfterDismissingSnackbar; + // `context` is used in `onCreate()`, `onOptionsItemSelected()`, and `onAddDomain()`. private Context context; @@ -284,17 +288,11 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo // 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 = () -> { - // Go home. - NavUtils.navigateUpFromSameTask(this); - }; + // Set the close flag. + closeActivityAfterDismissingSnackbar = true; - // 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); + // Dismiss the snackbar. + undoDeleteSnackbar.dismiss(); } else { // Go home. NavUtils.navigateUpFromSameTask(this); @@ -325,17 +323,11 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo } else { // The device is in single-paned mode and `DomainsListFragment` is displayed. // Dismiss the undo delete `SnackBar` if it is shown. if (undoDeleteSnackbar != null && undoDeleteSnackbar.isShown()) { - undoDeleteSnackbar.dismiss(); + // Set the close flag. + closeActivityAfterDismissingSnackbar = true; - // Create a `Runnable` to return to the main activity. - Runnable navigateHomeRunnable = () -> { - // Go home. - NavUtils.navigateUpFromSameTask(this); - }; - - // 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); + // Dismiss the snackbar. + undoDeleteSnackbar.dismiss(); } else { // Go home. NavUtils.navigateUpFromSameTask(this); @@ -400,6 +392,9 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo // Update the `ListView`. domainsListView.setAdapter(domainsPendingDeleteCursorAdapter); + // Get a handle for the activity. + Activity activity = this; + // Display a `Snackbar`. undoDeleteSnackbar = Snackbar.make(domainsListView, R.string.domain_deleted, Snackbar.LENGTH_LONG) .setAction(R.string.undo, (View v) -> { @@ -473,7 +468,7 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo // Delete the selected domain. domainsDatabaseHelper.deleteDomain(databaseIdToDelete); - // enable `deleteMenuItem` if the system was waiting for a `Snackbar` to be dismissed. + // 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 = () -> { @@ -497,10 +492,17 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo dismissingSnackbar = false; }; - // Run `enableDeleteMenuItemRunnable` after 100 milliseconds to make sure that the previous domain has been deleted from the database. + // 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); + } + break; } } @@ -541,15 +543,12 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo } // 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; + if ((undoDeleteSnackbar != null) && undoDeleteSnackbar.isShown()) { + // Set the close flag. + closeActivityAfterDismissingSnackbar = true; - // 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); + // Dismiss the snackbar. + undoDeleteSnackbar.dismiss(); } else { // Pass `onBackPressed()` to the system. super.onBackPressed(); @@ -579,15 +578,12 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo 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(); + if ((undoDeleteSnackbar != null) && undoDeleteSnackbar.isShown()) { + // Set the close flag. + closeActivityAfterDismissingSnackbar = true; - // 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); + // Dismiss the snackbar. + undoDeleteSnackbar.dismiss(); } else { // Pass `onBackPressed()` to the system. super.onBackPressed(); @@ -598,7 +594,7 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo @Override public void onAddDomain(AppCompatDialogFragment dialogFragment) { // Dismiss the undo delete snackbar if it is currently displayed. - if ((undoDeleteSnackbar != null) && (undoDeleteSnackbar.isShown())) { + if ((undoDeleteSnackbar != null) && undoDeleteSnackbar.isShown()) { undoDeleteSnackbar.dismiss(); }