1
I have a project with Navigationdrawer, on the main page I have a menu, and on the other fragments I inflate, only when I press the back of the navigationbar 'back button of the mobile phone', it closes the application and does not return 'or returns a page or a fragment', I wanted to press him to return and not close the app.
Follows the onBackPressed
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
Would you be able to give more details of the problem?
– viana
I have a project with Navigationdrawer, on the main page I have a menu, and on the other fragments I inflate, only when I press the back of the navigationbar 'back button of the mobile phone', it closes the application and does not return 'or returns a page or a fragment', I wanted to press him to return and not close the app.
– Ari Melo
I get it. Edit your question and put your comment on it.
– viana
Take a look at this reply at Soen: https://stackoverflow.com/a/14530879/2570426
– viana
Okay, I managed to get back to a chosen fragment, and then when it gets back to it it does nothing else, I wanted that when I got to the chosen fragment there yes it closed the app.
– Ari Melo
How you are creating the Fragments?
– Murillo Comino
In my
onNavigationItemSelected
is as follows.Fragment meuFragmento = null;
 boolean fragmentoSelec = false;
if (id == R.id.nav_quem_somos) {
 meuFragmento = new quemSomos();
 fragmentoSelec = true;
if (fragmentoSelec == true) {
 getSupportFragmentManager().beginTransaction().replace(R.id.content_principal, meuFragmento).commit();
 }
 DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
 drawer.closeDrawer(GravityCompat.START);
 return true;
– Ari Melo