Fix back while deleting bookmarks. https://redmine.stoutner.com/issues/292
authorSoren Stoutner <soren@stoutner.com>
Mon, 30 Jul 2018 21:38:26 +0000 (14:38 -0700)
committerSoren Stoutner <soren@stoutner.com>
Mon, 30 Jul 2018 21:38:26 +0000 (14:38 -0700)
app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksActivity.java
app/src/main/java/com/stoutner/privacybrowser/activities/DomainsActivity.java

index c822e0e78d1112bf7b94c9dafd786211ac540239..d556b305c11c6778a814eb599285d646791446ce 100644 (file)
@@ -96,12 +96,18 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
     // `oldFolderName` is used in `onCreate()` and `onSaveBookmarkFolder()`.
     private String oldFolderNameString;
 
-    // `moveBookmarkUpMenuItem` is used in `onCreate()` and `updateMoveIcons`.
+    // `moveBookmarkUpMenuItem` is used in `onCreate()` and `updateMoveIcons()`.
     private MenuItem moveBookmarkUpMenuItem;
 
-    // `moveBookmarkDownMenuItem` is used in `onCreate()` and `updateMoveIcons`.
+    // `moveBookmarkDownMenuItem` is used in `onCreate()` and `updateMoveIcons()`.
     private MenuItem moveBookmarkDownMenuItem;
 
+    // `bookmarksDeletedSnackbar` is used in `onCreate()`, `onOptionsItemSelected()`, and `onBackPressed()`.
+    private Snackbar bookmarksDeletedSnackbar;
+
+    // `closeActivityAfterDismissingSnackbar` is used in `onCreate()`, `onOptionsItemSelected()`, and `onBackPressed()`.
+    private boolean closeActivityAfterDismissingSnackbar;
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         // Disable screenshots if not allowed.
@@ -191,6 +197,9 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
             bookmarkCursor.close();
         });
 
+        // Get a handle for the activity.
+        Activity activity = this;
+
         // `MultiChoiceModeListener` handles long clicks.
         bookmarksListView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
             // Instantiate the common variables.
@@ -487,7 +496,7 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
                         }
 
                         // Show a SnackBar.
-                        Snackbar.make(findViewById(R.id.bookmarks_coordinatorlayout), snackbarMessage, Snackbar.LENGTH_LONG)
+                        bookmarksDeletedSnackbar = Snackbar.make(findViewById(R.id.bookmarks_coordinatorlayout), snackbarMessage, Snackbar.LENGTH_LONG)
                                 .setAction(R.string.undo, view -> {
                                     // Do nothing because everything will be handled by `onDismissed()` below.
                                 })
@@ -546,10 +555,23 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
 
                                         // Reset the deleting bookmarks flag.
                                         deletingBookmarks = false;
+
+                                        // Close the activity if back has been pressed.
+                                        if (closeActivityAfterDismissingSnackbar) {
+                                            // Update the bookmarks folder for the bookmarks drawer in `MainWebViewActivity`.
+                                            MainWebViewActivity.currentBookmarksFolder = "";
+
+                                            // Close the bookmarks drawer and reload the bookmarks `ListView` when returning to `MainWebViewActivity`.
+                                            MainWebViewActivity.restartFromBookmarksActivity = true;
+
+                                            // Return to `MainWebViewActivity`.
+                                            NavUtils.navigateUpFromSameTask(activity);
+                                        }
                                     }
-                                })
-                                //Show the `SnackBar`.
-                                .show();
+                                });
+
+                        //Show the `SnackBar`.
+                        bookmarksDeletedSnackbar.show();
 
                         // Close the `ActionBar`.
                         mode.finish();
@@ -612,14 +634,23 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
         switch (menuItemId) {
             case android.R.id.home:  // The home arrow is identified as `android.R.id.home`, not just `R.id.home`.
                 if (currentFolder.isEmpty()) {  // Currently in the home folder.
-                    // Update the bookmarks folder for the bookmarks drawer in `MainWebViewActivity`.
-                    MainWebViewActivity.currentBookmarksFolder = "";
-
-                    // Close the bookmarks drawer and reload the bookmarks `ListView` when returning to `MainWebViewActivity`.
-                    MainWebViewActivity.restartFromBookmarksActivity = true;
-
-                    // Return to `MainWebViewActivity`.
-                    NavUtils.navigateUpFromSameTask(this);
+                    // Go home.
+                    if ((bookmarksDeletedSnackbar != null) && bookmarksDeletedSnackbar.isShown()) {  // Close the bookmarks delete snackbar before going home.
+                        // Set the close flag.
+                        closeActivityAfterDismissingSnackbar = true;
+
+                        // Dismiss the snackbar.
+                        bookmarksDeletedSnackbar.dismiss();
+                    } else {  // Go home immediately.
+                        // Update the bookmarks folder for the bookmarks drawer in `MainWebViewActivity`.
+                        MainWebViewActivity.currentBookmarksFolder = "";
+
+                        // Close the bookmarks drawer and reload the bookmarks `ListView` when returning to `MainWebViewActivity`.
+                        MainWebViewActivity.restartFromBookmarksActivity = true;
+
+                        // Return to `MainWebViewActivity`.
+                        NavUtils.navigateUpFromSameTask(this);
+                    }
                 } else {  // Currently in a subfolder.
                     // Place the former parent folder in `currentFolder`.
                     currentFolder = bookmarksDatabaseHelper.getParentFolder(currentFolder);
@@ -651,14 +682,23 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
     @Override
     public void onBackPressed() {
         if (currentFolder.isEmpty()) {  // Currently in the home folder.
-            // Update the bookmarks folder for the bookmarks drawer in `MainWebViewActivity`.
-            MainWebViewActivity.currentBookmarksFolder = "";
+            // Go home.
+            if ((bookmarksDeletedSnackbar != null) && bookmarksDeletedSnackbar.isShown()) {  // Close the bookmarks delete snackbar before going home.
+                // Set the close flag.
+                closeActivityAfterDismissingSnackbar = true;
+
+                // Dismiss the snackbar.
+                bookmarksDeletedSnackbar.dismiss();
+            } else {  // Go home immediately.
+                // Update the bookmarks folder for the bookmarks drawer in `MainWebViewActivity`.
+                MainWebViewActivity.currentBookmarksFolder = "";
 
-            // Close the bookmarks drawer and reload the bookmarks `ListView` when returning to `MainWebViewActivity`.
-            MainWebViewActivity.restartFromBookmarksActivity = true;
+                // Close the bookmarks drawer and reload the bookmarks `ListView` when returning to `MainWebViewActivity`.
+                MainWebViewActivity.restartFromBookmarksActivity = true;
 
-            // Exit `BookmarksActivity`.
-            super.onBackPressed();
+                // Exit `BookmarksActivity`.
+                super.onBackPressed();
+            }
         } else {  // Currently in a subfolder.
             // Place the former parent folder in `currentFolder`.
             currentFolder = bookmarksDatabaseHelper.getParentFolder(currentFolder);
index 80c5d4a13e48fee60e83306ee8d180fbc98da33b..a56e7184b6106f7277e6675dade66f4339ccd323 100644 (file)
@@ -541,7 +541,7 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
             }
 
             // Dismiss the undo delete SnackBar if it is shown.
-            if (undoDeleteSnackbar != null && undoDeleteSnackbar.isShown()) {
+            if ((undoDeleteSnackbar != null) && undoDeleteSnackbar.isShown()) {
                 undoDeleteSnackbar.dismiss();
 
                 // Create a runnable to return to the main activity.
@@ -579,7 +579,7 @@ 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()) {
+            if ((undoDeleteSnackbar != null) && undoDeleteSnackbar.isShown()) {
                 undoDeleteSnackbar.dismiss();
 
                 // Create a runnable to return to the main activity.
@@ -598,7 +598,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();
         }