Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f696a5a

Browse files
committed
Fixes #857 - Close Main Navigation Menu with Back button
1 parent 85704b4 commit f696a5a

File tree

2 files changed

+56
-13
lines changed

2 files changed

+56
-13
lines changed

app/src/main/java/org/gnucash/android/ui/common/BaseDrawerActivity.java

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,11 @@ public void onConfigurationChanged(Configuration newConfig) {
210210
@Override
211211
public boolean onOptionsItemSelected(MenuItem item) {
212212
if (item.getItemId() == android.R.id.home){
213-
if (!mDrawerLayout.isDrawerOpen(mNavigationView))
213+
if (!isNavigationViewOpen()) {
214214
mDrawerLayout.openDrawer(mNavigationView);
215-
else
216-
mDrawerLayout.closeDrawer(mNavigationView);
215+
} else {
216+
closeNavigationView();
217+
}
217218
return true;
218219
}
219220

@@ -286,7 +287,7 @@ protected void onDrawerMenuItemClicked(int itemId) {
286287
UserVoice.launchUserVoice(this);
287288
break;
288289
}
289-
mDrawerLayout.closeDrawer(mNavigationView);
290+
closeNavigationView();
290291
}
291292

292293
@Override
@@ -319,7 +320,7 @@ public boolean onMenuItemClick(MenuItem item) {
319320
Intent intent = new Intent(this, PreferenceActivity.class);
320321
intent.setAction(PreferenceActivity.ACTION_MANAGE_BOOKS);
321322
startActivity(intent);
322-
mDrawerLayout.closeDrawer(mNavigationView);
323+
closeNavigationView();
323324
return true;
324325
}
325326
BooksDbAdapter booksDbAdapter = BooksDbAdapter.getInstance();
@@ -332,9 +333,17 @@ public boolean onMenuItemClick(MenuItem item) {
332333
return true;
333334
}
334335

335-
public void onClickAppTitle(View view){
336+
protected void onClickAppTitle(View view) {
337+
338+
closeNavigationView();
339+
340+
// Do not launch AccountsActivity to stay on current Activity
341+
// AccountsActivity.start(this);
342+
}
343+
344+
protected void closeNavigationView() {
345+
336346
mDrawerLayout.closeDrawer(mNavigationView);
337-
AccountsActivity.start(this);
338347
}
339348

340349
public void onClickBook(View view){
@@ -354,4 +363,32 @@ public void onClickBook(View view){
354363

355364
popup.show();
356365
}
366+
367+
@Override
368+
public void onBackPressed() {
369+
370+
if (isNavigationViewOpen()) {
371+
// The main navigation menu is open
372+
373+
// Close the main navigation menu
374+
// mDrawerLayout.closeDrawer(mNavigationView);
375+
onClickAppTitle(getCurrentFocus());
376+
377+
} else {
378+
// The main navigation menu is closed
379+
380+
// Close the Activity
381+
super.onBackPressed();
382+
}
383+
}
384+
385+
/**
386+
* Return true if main navigation menu is open
387+
*
388+
* @return true if main navigation menu is open
389+
*/
390+
protected boolean isNavigationViewOpen() {
391+
392+
return mDrawerLayout.isDrawerOpen(mNavigationView);
393+
}
357394
}

app/src/main/java/org/gnucash/android/ui/settings/PreferenceActivity.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,18 @@ private void loadFragment(Fragment fragment) {
127127
public boolean onOptionsItemSelected(MenuItem item) {
128128
switch (item.getItemId()) {
129129
case android.R.id.home:
130-
android.app.FragmentManager fm = getFragmentManager();
131-
if (fm.getBackStackEntryCount() > 0) {
132-
fm.popBackStack();
133-
} else {
134-
finish();
135-
}
130+
// User clicked on the "home" button (i.e. left arrow in the ActionBar)
131+
132+
// Handle as it was a Back Button press
133+
onBackPressed();
134+
135+
// android.app.FragmentManager fm = getFragmentManager();
136+
// if (fm.getBackStackEntryCount() > 0) {
137+
// fm.popBackStack();
138+
// } else {
139+
// finish();
140+
// }
141+
136142
return true;
137143

138144
default:

0 commit comments

Comments
 (0)