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 get last fragment from backstack. However, navigating from fragment to another fragment should not be possible.
More Clarity:
I have one Activity (with a navigation drawer) and 2 Fragments. From the navigation drawer I can go to both fragments as many times as I want. But when I hit the back button, I want always to go back to the main_activity.xml
.