+ @Override
+ public void onBackPressed() {
+ if (isDrawerOpen()) {
+ closeNavDrawer();
+ return;
+ }
+ super.onBackPressed();
+ }
+
+ /**
+ * checks if the drawer exists and is opened.
+ *
+ * @return <code>true</code> if the drawer is open, else <code>false</code>
+ */
+ public boolean isDrawerOpen() {
+ if(mDrawerLayout != null) {
+ return mDrawerLayout.isDrawerOpen(GravityCompat.START);
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * closes the navigation drawer.
+ */
+ public void closeNavDrawer() {
+ if(mDrawerLayout != null) {
+ mDrawerLayout.closeDrawer(GravityCompat.START);
+ }
+ }
+