From f696a5a4a49ae625282e56374d01c2fb816e2a21 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sun, 5 Jan 2020 23:33:53 +0100 Subject: [PATCH] Fixes #857 - Close Main Navigation Menu with Back button --- .../android/ui/common/BaseDrawerActivity.java | 51 ++++++++++++++++--- .../ui/settings/PreferenceActivity.java | 18 ++++--- 2 files changed, 56 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/ui/common/BaseDrawerActivity.java b/app/src/main/java/org/gnucash/android/ui/common/BaseDrawerActivity.java index 83c409d95..3e4638fc4 100644 --- a/app/src/main/java/org/gnucash/android/ui/common/BaseDrawerActivity.java +++ b/app/src/main/java/org/gnucash/android/ui/common/BaseDrawerActivity.java @@ -210,10 +210,11 @@ public void onConfigurationChanged(Configuration newConfig) { @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home){ - if (!mDrawerLayout.isDrawerOpen(mNavigationView)) + if (!isNavigationViewOpen()) { mDrawerLayout.openDrawer(mNavigationView); - else - mDrawerLayout.closeDrawer(mNavigationView); + } else { + closeNavigationView(); + } return true; } @@ -286,7 +287,7 @@ protected void onDrawerMenuItemClicked(int itemId) { UserVoice.launchUserVoice(this); break; } - mDrawerLayout.closeDrawer(mNavigationView); + closeNavigationView(); } @Override @@ -319,7 +320,7 @@ public boolean onMenuItemClick(MenuItem item) { Intent intent = new Intent(this, PreferenceActivity.class); intent.setAction(PreferenceActivity.ACTION_MANAGE_BOOKS); startActivity(intent); - mDrawerLayout.closeDrawer(mNavigationView); + closeNavigationView(); return true; } BooksDbAdapter booksDbAdapter = BooksDbAdapter.getInstance(); @@ -332,9 +333,17 @@ public boolean onMenuItemClick(MenuItem item) { return true; } - public void onClickAppTitle(View view){ + protected void onClickAppTitle(View view) { + + closeNavigationView(); + + // Do not launch AccountsActivity to stay on current Activity +// AccountsActivity.start(this); + } + + protected void closeNavigationView() { + mDrawerLayout.closeDrawer(mNavigationView); - AccountsActivity.start(this); } public void onClickBook(View view){ @@ -354,4 +363,32 @@ public void onClickBook(View view){ popup.show(); } + + @Override + public void onBackPressed() { + + if (isNavigationViewOpen()) { + // The main navigation menu is open + + // Close the main navigation menu +// mDrawerLayout.closeDrawer(mNavigationView); + onClickAppTitle(getCurrentFocus()); + + } else { + // The main navigation menu is closed + + // Close the Activity + super.onBackPressed(); + } + } + + /** + * Return true if main navigation menu is open + * + * @return true if main navigation menu is open + */ + protected boolean isNavigationViewOpen() { + + return mDrawerLayout.isDrawerOpen(mNavigationView); + } } diff --git a/app/src/main/java/org/gnucash/android/ui/settings/PreferenceActivity.java b/app/src/main/java/org/gnucash/android/ui/settings/PreferenceActivity.java index 10cf12b29..965b55c3f 100644 --- a/app/src/main/java/org/gnucash/android/ui/settings/PreferenceActivity.java +++ b/app/src/main/java/org/gnucash/android/ui/settings/PreferenceActivity.java @@ -127,12 +127,18 @@ private void loadFragment(Fragment fragment) { public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: - android.app.FragmentManager fm = getFragmentManager(); - if (fm.getBackStackEntryCount() > 0) { - fm.popBackStack(); - } else { - finish(); - } + // User clicked on the "home" button (i.e. left arrow in the ActionBar) + + // Handle as it was a Back Button press + onBackPressed(); + +// android.app.FragmentManager fm = getFragmentManager(); +// if (fm.getBackStackEntryCount() > 0) { +// fm.popBackStack(); +// } else { +// finish(); +// } + return true; default: