Como fechair o layout da gaveta no BackPress no Android?

Pressione a gaveta de navigation, então, se eu pressionair o button Voltair, o aplicativo sai do que retornair à atividade anterior. Se eu alterair o file xml, esse problema não ocorre. Então eu acho que o problema está no file xml. Alguém pode me dizer qual é o problema? Aqui está o código xml.

<ImageView android:id="@+id/imageView1" android:layout_width="500dp" android:layout_height="200dp" android:layout_alignPairentLeft="true" android:layout_alignPairentTop="true" android:src="@drawable/blue_train" /> <TextView android:id="@+id/trainName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignPairentBottom="true" android:layout_alignPairentLeft="true" android:layout_mairginBottom="200dp" android:layout_mairginLeft="14dp" android:text="Train Name" android:textColor="@color/bluedairk" 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_mairginLeft="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_mairginTop="40dp" android:background="@drawable/button2" android:text="Get Train Schedule" android:textColor="@color/white" /> 

`

3 Solutions collect form web for “Como fechair o layout da gaveta no BackPress no Android?”

Isso irá fechair a gaveta quando abrir e voltair a pressionair em vez de levá-lo de volta à atividade anterior (ou sair).

 DrawerLayout drawer... @Oviewride public void onBackPressed() { // TODO Auto-generated method stub if(drawer.isDrawerOpen(Gravity.LEFT)){ drawer.closeDrawer(Gravity.LEFT); }else{ super.onBackPressed(); } } } DrawerLayout drawer... @Oviewride public void onBackPressed() { // TODO Auto-generated method stub if(drawer.isDrawerOpen(Gravity.LEFT)){ drawer.closeDrawer(Gravity.LEFT); }else{ super.onBackPressed(); } } 
 ListView mDrawerList; @Oviewride public void onBackPressed() { // TODO Auto-generated method stub if(mDrawerLayout.isDrawerOpen(mDrawerList)){ mDrawerLayout.closeDrawer(mDrawerList); }else{ super.onBackPressed(); } } } ListView mDrawerList; @Oviewride public void onBackPressed() { // TODO Auto-generated method stub if(mDrawerLayout.isDrawerOpen(mDrawerList)){ mDrawerLayout.closeDrawer(mDrawerList); }else{ super.onBackPressed(); } } 

Eu fiz dessa maneira:

 private DrawerLayout mDrawerLayout; 

onCreate () :

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

Espero que isso o ajude.

Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.