1

i have a navigation drawer which should close on backpress this works perfectly but when the navigation drawer closes and there is a second time back button press the app closes instead of going to home screen

here is the code i am trying

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

        if(drawerLayout.isDrawerOpen(Gravity.LEFT)){
            drawerLayout.closeDrawer(Gravity.LEFT);
        }else{
            //super.onBackPressed();
             Intent mainActivity = new Intent(Intent.ACTION_MAIN);
             mainActivity.addCategory(Intent.CATEGORY_HOME);
             mainActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             startActivity(mainActivity);

        }

how can we close the navigation drawer then go directly to home screen/ main activity

i donot wan to use

Intent i = new Intent(AccountActivity.this, HomeActivity.class);
                    startActivity(i);
1234567
  • 2,226
  • 4
  • 24
  • 69
  • So you have a NavigationDrawer-based app with several activities? What are the rest of the activities (besides the one with the drawer) used for and can't you just put them in fragments that are added/removed in/from the NavigationDrawer activity. – vap78 Jan 24 '15 at 12:24

2 Answers2

1

While starting the another activity from the home dont use the finish() method.

//remove the finish method from the home screen.

1

Firstly, please read and understand my answer here to clear your MainActivity's backstacks. My method will not work until you understand that.

So you cannot to close your app directly, unless you back to home screen first and close the home screen's Activity (see this question).

CurrentActivity:

if(drawerLayout.isDrawerOpen(Gravity.LEFT)) {
    drawerLayout.closeDrawer(Gravity.LEFT);
}else{
// After you understood my answer above (I link it with "here"),
// you just need this call:
    startActivity(new Intent(CurrentActivity.this, HomeScreenActivity.class));
}

Notice: Calling finish() in the onResume() method might cause a problem.

Call finish() in the onResume() method inside your HomeScreenActivity:

@Override
protected void onResume() {
    super.onResume();
    HomeScreenActivity.this.finish();
}
Community
  • 1
  • 1
Anggrayudi H
  • 14,977
  • 11
  • 54
  • 87