Event onBackPressed

Asked

Viewed 394 times

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?

  • 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.

  • I get it. Edit your question and put your comment on it.

  • Take a look at this reply at Soen: https://stackoverflow.com/a/14530879/2570426

  • 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.

  • How you are creating the Fragments?

  • 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;

Show 2 more comments

1 answer

0


Hello, you can try the following:

   public void onBackPressed() {
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    //retorna o drawer
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);

        // retorna todos os fragments que estão em backStack
    } else if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
        getSupportFragmentManager().popBackStack();
        if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
            //Volta o titulo pra Activity
            toolbar.setTitle("Activity");

        }

    }
    //fecha a aplicação, aqui você pode fazer voltar para alguma activity
    else {
        super.onBackPressed();
    }
}

When to call the Add Ragments at all addToBackStack(null) as follows:

getSupportFragmentManager().beginTransaction().replace(R.id.content_principal, meuFragmento).addToBackStack(null).commit();

Example on github: onBackPressed

  • Still closing the app.

  • Hello friend, I edited, I tested here and for me it worked perfectly, try to do like I sent now.

  • If you want I send you the simple example I made for testing.

  • Send if possible,

  • But for you the way I put it, it’s still not turning Fragment? and neither does Drawer close when you click the back button? I put the example link in the editing of my reply

  • Murilo, if I call the fragment from the menu and come back it works and goes back to the previous screen and then close the normal app, but I put a few buttons on the main screen calling the same fragments, and when I come back it closes, does not go back to the previous.

  • Solved, put this line addToBackStack(null) on the buttons too.

  • @Arimelo forgot to mention this. You need to put addToBackStack() on all fragments to realize the return option. If you have any more questions, I sent you an email on Saturday just answer me for it I help. Hug

  • Blz @Murillocomino, the only thing I noticed was that, when returning to the home screen it is not changing the name in the Toolbar bar, it is getting the name of the fragment that opens.

  • @Arimelo, I edited my answer, if you are calling Toolbar’s titles inside Fragment, changing in your Mainactivity the onBackPressed() on the part of else if(...) as edited in the answer will work, as well as tested and updated the project on github.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.