From 5f42cf731a90231386ea4757c6a8964a48da042e Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Fri, 25 Nov 2016 16:11:23 -0700 Subject: [PATCH] Navigate up folders in the Bookmarks activity when the back button is pressed. Fixes https://redmine.stoutner.com/issues/60. --- .../privacybrowser/activities/Bookmarks.java | 25 +++++++++++++------ .../activities/MainWebView.java | 6 ++--- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/Bookmarks.java b/app/src/main/java/com/stoutner/privacybrowser/activities/Bookmarks.java index 78cb806e..5c059ec0 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/Bookmarks.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/Bookmarks.java @@ -107,12 +107,11 @@ public class Bookmarks extends AppCompatActivity implements CreateBookmark.Creat // Display the home arrow on `SupportActionBar`. appBar = getSupportActionBar(); - assert appBar != null;// This assert removes the incorrect warning in Android Studio on the following line that appBar might be null. + assert appBar != null;// This assert removes the incorrect warning in Android Studio on the following line that `appBar` might be null. appBar.setDisplayHomeAsUpEnabled(true); - // Initialize the database handler and the ListView. - // `this` specifies the context. The two `null`s do not specify the database name or a `CursorFactory`. + // Initialize the database handler and the ListView. `this` specifies the context. The two `null`s do not specify the database name or a `CursorFactory`. // The `0` is to specify a database version, but that is set instead using a constant in `BookmarksDatabaseHelper`. bookmarksDatabaseHelper = new BookmarksDatabaseHelper(this, null, null, 0); bookmarksListView = (ListView) findViewById(R.id.bookmarks_listview); @@ -123,8 +122,7 @@ public class Bookmarks extends AppCompatActivity implements CreateBookmark.Creat // Display the bookmarks in the ListView. updateBookmarksListView(currentFolder); - // Set a listener so that tapping a list item loads the URL. We need to store the activity - // in a variable so that we can return to the parent activity after loading the URL. + // Set a listener so that tapping a list item loads the URL. We need to store the activity in a variable so that we can return to the parent activity after loading the URL. final Activity bookmarksActivity = this; bookmarksListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override @@ -523,13 +521,13 @@ public class Bookmarks extends AppCompatActivity implements CreateBookmark.Creat switch (menuItemId) { case android.R.id.home: - // Exit Bookmarks if currently at the home folder. - if (currentFolder.isEmpty()) { + if (currentFolder.isEmpty()) { // Exit Bookmarks if currently in the home folder. NavUtils.navigateUpFromSameTask(this); } else { // Navigate up one folder. // Place the former parent folder in `currentFolder`. currentFolder = bookmarksDatabaseHelper.getParentFolder(currentFolder); + // Exit Bookmarks if currently in the home folder. updateBookmarksListView(currentFolder); } break; @@ -557,6 +555,19 @@ public class Bookmarks extends AppCompatActivity implements CreateBookmark.Creat return true; } + @Override + public void onBackPressed() { + if (currentFolder.isEmpty()) { // Exit Bookmarks if currently in the home folder. + super.onBackPressed(); + } else { // Navigate up one folder. + // Place the former parent folder in `currentFolder`. + currentFolder = bookmarksDatabaseHelper.getParentFolder(currentFolder); + + // Reload the `ListView`. + updateBookmarksListView(currentFolder); + } + } + @Override public void onCreateBookmark(AppCompatDialogFragment dialogFragment) { // Get the `EditText`s from the `createBookmarkDialogFragment` and extract the strings. diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebView.java b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebView.java index 5ac470bb..952f5aae 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebView.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebView.java @@ -1417,11 +1417,9 @@ public class MainWebView extends AppCompatActivity implements NavigationView.OnN mainWebView.clearHistory(); } - // Override onBackPressed to handle the navigation drawer and mainWebView. + // Override `onBackPressed` to handle the navigation drawer and `mainWebView`. @Override public void onBackPressed() { - final WebView mainWebView = (WebView) findViewById(R.id.mainWebView); - // Close the navigation drawer if it is available. GravityCompat.START is the drawer on the left on Left-to-Right layout text. if (drawerLayout.isDrawerVisible(GravityCompat.START)) { drawerLayout.closeDrawer(GravityCompat.START); @@ -1430,7 +1428,7 @@ public class MainWebView extends AppCompatActivity implements NavigationView.OnN if (mainWebView.canGoBack()) { mainWebView.goBack(); } else { - // Pass onBackPressed to the system. + // Pass `onBackPressed()` to the system. super.onBackPressed(); } } -- 2.45.2