Reputation: 64012
Android provides user navigation using back icon or icon on ActionBar
getActionBar().setDisplayHomeAsUpEnabled(true);
and AndroidManifest.xml has
<!--
since 4.0
android:parentActivityName=".MainActivity" >
-->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.MainActivity" />
Now if my Activity has 3 fragments 1-2-3, how to enable back navigation to come back 3->2->1 ?
When user presses back, how to go to previous fragment, not activity?
Upvotes: 0
Views: 1099
Reputation: 18974
Note that you may have several accessible fragments inside an application. So you need a robust and infallible solution. I found a complete and explained solution at the page Managing the Fragment Back Stack with the related code in GitHub.
Upvotes: 0
Reputation: 1784
You can pop the fragment by name. While adding fragments to the back stack, just give them a name.
fragmentTransaction.addToBackStack("fragB");
fragmentTransaction.addToBackStack("fragC");
Then in Fragment_C, pop the back stack using the name ie.. fragB and include POP_BACK_STACK_INCLUSIVE
someButtonInC.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fm = getActivity()
.getSupportFragmentManager();
fm.popBackStack ("fragB", FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
});
Upvotes: 4