12

I press the navigation drawer, then if I press back button, the app exits rather than returning to the previous activity. If I change the xml file, then this problem doesn't occur. So I think the problem is in the xml file. Can anyone tell me what is the problem? Here's the xml code.`

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="500dp"
    android:layout_height="200dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/blue_train" />

<TextView
    android:id="@+id/trainName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="200dp"
    android:layout_marginLeft="14dp"
    android:text="Train Name"
    android:textColor="@color/bluedark"
    android:textSize="15sp" />

<EditText
    android:id="@+id/etName"
    android:layout_width="150dp"
    android:layout_height="40dp"
    android:layout_alignBaseline="@+id/trainName"
    android:layout_alignBottom="@+id/trainName"
    android:layout_marginLeft="30dp"
    android:layout_toRightOf="@+id/trainName"
    android:background="@drawable/line"
    android:ems="10" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/getS"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/etName"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="40dp"
    android:background="@drawable/button2"
    android:text="Get Train Schedule"
    android:textColor="@color/white" />

`

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
alu
  • 277
  • 1
  • 3
  • 12
  • When you say you change the XML and the problem goes away - in what way do you change it? – SDJMcHattie Mar 04 '14 at 21:35
  • I figured out, if I remove the EditText, it works. But why doesn't it work when I have the EditText? – alu Mar 04 '14 at 21:41
  • Cause the EditText was requesting focus, if EditText isn't focused, then there's no problem – alu Mar 04 '14 at 21:45

4 Answers4

38

This will close the drawer when it's open and back is pressed rather than taking you back to the previous activity (or exiting).

DrawerLayout drawer...

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub

    if(drawer.isDrawerOpen(Gravity.LEFT)){
        drawer.closeDrawer(Gravity.LEFT);
    }else{
        super.onBackPressed();
    }
}
NameSpace
  • 10,009
  • 3
  • 39
  • 40
  • You can also do it for multiple drawers. if(drawer.isDrawerOpen(Gravity.RIGHT)) drawer.closeDrawer(Gravity.RIGHT); – Kalel Wade Sep 09 '14 at 16:37
2
ListView mDrawerList;

@Override
public void onBackPressed() {
// TODO Auto-generated method stub

if(mDrawerLayout.isDrawerOpen(mDrawerList)){
    mDrawerLayout.closeDrawer(mDrawerList);
}else{
    super.onBackPressed();
}
}
ultimate
  • 717
  • 2
  • 10
  • 20
1

I have done this way:

private DrawerLayout mDrawerLayout;

onCreate():

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);  
mDrawerLayout.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);

Hope this will help you.

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
0

@Hiren Patel's answer is the most elegant so far, but you should set:

mDrawerLayout.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);

Or

mDrawerLayout.descendantFocusability = ViewGroup.FOCUS_BEFORE_DESCENDANTS

Just so you don't block the other child views from receiving focus, and thus blocking the soft keyboard for instance.