If you want back button functionality you have to add the Fragment
to the back stack in the transaction.
FragmentManager manger = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.flFragmentContainer, fragment);
transaction.addToBackStack(null); // Add fragment to back stack.
transaction.commit();
But this is not recommended. The reason they suggest you use an extra Activity
is so you can build the navigation stack with Activities
instead of fragments. Building a navigation stack with Fragments
can become problematic very quickly. Activities
are just supposed to be container for Fragments
. As such in any app you are going to have many Activities
that do not contain anything aside from a Fragment
and these Activities
are just used to build a navigation stack. In really big apps I tend to write one abstract base Activity
that implements all the base features you need and reuse this on Activity
as much as possible.