// 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);
// 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
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;
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.
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);
if (mainWebView.canGoBack()) {
mainWebView.goBack();
} else {
- // Pass onBackPressed to the system.
+ // Pass `onBackPressed()` to the system.
super.onBackPressed();
}
}