Skip to content

Commit

Permalink
Fixes codinguser#855 - Back button closes the Main Navigation Menu an…
Browse files Browse the repository at this point in the history
…d stay on current Activity
  • Loading branch information
JeanGarf committed Jan 6, 2020
1 parent 5bb9560 commit 21b6e9f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -286,7 +287,7 @@ protected void onDrawerMenuItemClicked(int itemId) {
UserVoice.launchUserVoice(this);
break;
}
mDrawerLayout.closeDrawer(mNavigationView);
closeNavigationView();
}

@Override
Expand Down Expand Up @@ -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();
Expand All @@ -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){
Expand All @@ -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);
}
}

0 comments on commit 21b6e9f

Please sign in to comment.