Skip to content

Commit

Permalink
Merge pull request codinguser#866 from JeanGarf/#857_v2_home_as_back_btn
Browse files Browse the repository at this point in the history
Fixes codinguser#857 - Close Main Navigation Menu with Back button
  • Loading branch information
codinguser authored Dec 3, 2020
2 parents 3ffeb5e + f696a5a commit 4a97b14
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 13 deletions.
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 4a97b14

Please sign in to comment.