Redab0 Redab0 - 1 day ago 4
Android Question

How to probably handle Back Button

I know this is a common question but there isn't one thread with the right/efficient answer.

I have one MainActivity with 6 different fragments inflating in it, I have a drawer menu, and a startActivity that holds a flash screen with the logo and the app name in it at start

my problems are the following:


  1. When I'm in the main activity and I open the drawer then hit the back button it closes the menu then when i open it again and hit the back button it goes to the startActivity not close the menu

  2. when I'm in one of the fragments and the drawer menu is open and I hit the back button it keeps the menu open and goes to the previous fragment/activity

  3. I never want my backButton to take me to the startActivity... is that possible?



here is my onBackPressed() method in the MainActivity

@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);



if (drawer.isDrawerOpen(GravityCompat.END )) {
drawer.closeDrawer(GravityCompat.START);}

else
super.onBackPressed();


}


ps: if you have no idea what I'm talking about or don't know the answer, ALTHOUGH appreciated please don't type any answer or a wrong answer it will just make this a longer thread than needed.

Thank you :)

Answer

With "splashscreens" you can delete them from the "back-stack" by setting the android:noHistory attribute to "true" in the relevant <activity> entries in your AndroidManifest.xml file. For example:

<activity
    android:name=".startActivity"
    android:noHistory="true" />
Comments