From 21b6e9f5f750ee27c8ba6876640430efb407a7e6 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Mon, 6 Jan 2020 01:51:58 +0100 Subject: [PATCH] Fixes #855 - Back button closes the Main Navigation Menu and stay on current Activity --- .../android/ui/account/AccountsActivity.java | 64 +++++++++++-------- .../android/ui/common/BaseDrawerActivity.java | 51 +++++++++++++-- 2 files changed, 81 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/ui/account/AccountsActivity.java b/app/src/main/java/org/gnucash/android/ui/account/AccountsActivity.java index c31a372ee..1b473dd38 100644 --- a/app/src/main/java/org/gnucash/android/ui/account/AccountsActivity.java +++ b/app/src/main/java/org/gnucash/android/ui/account/AccountsActivity.java @@ -431,45 +431,55 @@ protected void onDestroy() { @Override public void onBackPressed() { - // Get Preference about double back button press to exit - boolean prefShallUseDoubleBackPressToExit = PreferenceManager.getDefaultSharedPreferences(this) - .getBoolean(getString(R.string.key_use_double_back_button_press_to_quit), - true); + if (isNavigationViewOpen()) { + // The main navigation menu is open - if (mDoubleBackButtonPressedOnce || !prefShallUseDoubleBackPressToExit) { - // BackPress button has already been pressed recently OR shall not use double back press to exit + // Close the main navigation menu + super.onBackPressed(); + + } else { + // The main navigation menu is closed - // - // Do not show the Toast anymore - // + // Get Preference about double back button press to exit + boolean prefShallUseDoubleBackPressToExit = PreferenceManager.getDefaultSharedPreferences(this) + .getBoolean(getString(R.string.key_use_double_back_button_press_to_quit), + true); - if (mToast != null) { - // There is a Toast + if (mDoubleBackButtonPressedOnce || !prefShallUseDoubleBackPressToExit) { + // BackPress button has already been pressed recently OR shall not use double back press to exit + // // Do not show the Toast anymore - mToast.cancel(); + // - } else { - // There is no Toast + if (mToast != null) { + // There is a Toast - // NTD - } + // Do not show the Toast anymore + mToast.cancel(); - // Perform BackPress - super.onBackPressed(); + } else { + // There is no Toast - } else { - // BackPress button has been pressed for the first time AND shall use double back press to exit + // NTD + } + + // Perform BackPress + super.onBackPressed(); - // Notice that button has been pressed once - this.mDoubleBackButtonPressedOnce = true; + } else { + // BackPress button has been pressed for the first time AND shall use double back press to exit + + // Notice that button has been pressed once + this.mDoubleBackButtonPressedOnce = true; - // Show a message to explain that user must press again to exit - mToast.show(); + // Show a message to explain that user must press again to exit + mToast.show(); - // After two seconds, it is not more considered as already pressed - mHandler.postDelayed(mResetDoubleBackPressedStatusRunnable, - 2000); + // After two seconds, it is not more considered as already pressed + mHandler.postDelayed(mResetDoubleBackPressedStatusRunnable, + 2000); + } } } 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); + } }