Adrime Adrime - 1 year ago 79
Android Question

Drawer Layout not closing on back pressed (depending on support-v4 lib)

I'm following the Creating a Navigation Drawer tutorial (Android Training).

I have downloaded the sample app and it work as intended. I open the drawer, press back and it gets closed.

The problem comes when I replace the

(originally with a size of 523 KB) with the updated version (revision 20) in my
(with size 741 KB). After the replacement if I open the drawer and then press the back button of the device the app gets closed instead of closing the drawer.

How can I solve that? Should I keep the old android-support-v4.jar version or should I programmatically intercept the back button closing the drawer when needed?

UPDATE: I've continue testing the problem, to do so I built the project in AndroidStudio. After copy the sample app (src, res and update the manifest) ran it: The back button closes the app even if the DrawerLayout is open.
Then I modified the build.gradle file changing the line:

compile ''


compile ''

And the problem disapeared: The back button first closes the drawer and then closes the app

Full code for the activity (Link to the Android Training Source Code):

public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mPlanetTitles;

protected void onCreate(Bundle savedInstanceState) {

mTitle = mDrawerTitle = getTitle();
mPlanetTitles = getResources().getStringArray(R.array.planets_array);
mDrawerLayout = (DrawerLayout) findViewById(;
mDrawerList = (ListView) findViewById(;

// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// set up the drawer's list view with items and click listener
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mPlanetTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

// enable ActionBar app icon to behave as action to toggle nav drawer

// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
//supportInvalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()

public void onDrawerOpened(View drawerView) {
//supportInvalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()

if (savedInstanceState == null) {

public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(, menu);
return super.onCreateOptionsMenu(menu);

/* Called whenever we call invalidateOptionsMenu() */
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
return super.onPrepareOptionsMenu(menu);

public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
// Handle action buttons
switch(item.getItemId()) {
// create intent to perform web search for this planet
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, getActionBar().getTitle());
// catch event that there's no activity to handle intent
if (intent.resolveActivity(getPackageManager()) != null) {
} else {
Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show();
return true;
return super.onOptionsItemSelected(item);

/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

private void selectItem(int position) {
// update the main content by replacing fragments
Fragment fragment = new PlanetFragment();
Bundle args = new Bundle();
args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);

FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(, fragment).commit();

// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);

public void setTitle(CharSequence title) {
mTitle = title;

* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...

protected void onPostCreate(Bundle savedInstanceState) {
// Sync the toggle state after onRestoreInstanceState has occurred.

public void onConfigurationChanged(Configuration newConfig) {
// Pass any configuration change to the drawer toggls

* Fragment that appears in the "content_frame", shows a planet
public static class PlanetFragment extends Fragment {
public static final String ARG_PLANET_NUMBER = "planet_number";

public PlanetFragment() {
// Empty constructor required for fragment subclasses

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
int i = getArguments().getInt(ARG_PLANET_NUMBER);
String planet = getResources().getStringArray(R.array.planets_array)[i];

int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()),
"drawable", getActivity().getPackageName());
((ImageView) rootView.findViewById(;
return rootView;

Answer Source

In setUpNavDrawer



public void onBackPressed()
  if (mDrawerLayout.isOpen())
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download