Navigation Drawer - back button

Asked

Viewed 779 times

1

I’m trying to get the button back or back from android keep the sequence of Fragments that I’m rendering as chosen option in my side menu (Navigation Drawer), but I’m not succeeding, it always closes the app when I click back and does not return to previous Fragment where it was. I’m doing it this way:

@Override
public boolean onNavigationItemSelected(MenuItem item) {

    int id = item.getItemId();

    switch (id) {
        case R.id.nav_home:
            myFragment = new SliderFragment();
            break;
        case R.id.nav_program:
            myFragment = new ProgramFragment();
            break;
        case R.id.nav_ticket:
            myFragment = new TicketFragment();
            break;
        case R.id.nav_togo:
            myFragment = new HowtogoFragment();
            break;
        case R.id.nav_map:
            myFragment = new MapFragment();
            break;
        case R.id.nav_social:
            myFragment = new SocialFragment();
            break;
        case R.id.nav_festival:
            myFragment = new FestivalFragment();
            break;
        case R.id.nav_contact:
            myFragment = new ContactFragment();
            break;
    }

    setTitle(item.getTitle(););

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);

    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager
            .beginTransaction()
            .replace(R.id.content_frame, myFragment)
            .addToBackStack(null)
            .commit();

    return true;
}

And I’m not getting the click of the button come back like this:

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

    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

How could I keep the button coming back normal? Always returning to the previous Fragment, with the title of the action bar and the corresponding selected menu item?

1 answer

-1

Instead of Fragmentmanager, use Fragmenttransaction.

Example:

 import android.support.v4.app.FragmentTransaction;

 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                    ft.replace(R.id.content_frame, myFragment);
                    ft.addToBackStack(null);
                    ft.commit();
  • What is the difference between this code and the one in the question?

  • Fragmentmanager serves to interact with Fragments within an Activity. Fragmenttransaction for a set of operations with Fragments, such as: add, remove and replace. I never used Fragmentmanager, because the times I needed I used Fragmenttransaction directly.

  • Rafael, both codes are using FragmentTransaction, the difference is that you use an intermediate variable ft while AP uses method chaining(method chaining) to access directly the FragmentTransaction.

Browser other questions tagged

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