vendredi 15 mai 2015

How to make back button for fragments go back to the main_activity layout?

I have a main activity and 2 fragments. In my main activity I have navigation drawer that navigates through the fragments. I handle the back button pressed like this:

@Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() > 0) {
        getFragmentManager().popBackStackImmediate();
    } else {
        super.onBackPressed();
    }
}

After navigating between the two fragments, when I hit the back button I always to back to the previous fragment until the stack is empty.

BUT what I want to do is return to my main activity from any fragment at all times. Can anyone help me? Thanks.

Aucun commentaire:

Enregistrer un commentaire