The component that is responsible for interacting with fragments from your current activity is fragment manager. We can use the FragmentManager form in two different imports:
- android.app.FragmentManager
- android.support.v4.app.Fragment
Import from the Android support library is recommended.
To perform a series of edit operations, start the fragment transaction using the beginTransaction() method. It will return an instance of transaction. To add a fragment (usually the first), use the add method, like in our example. The method takes the same arguments, but replaces the current fragment if already added. If we are planning to navigate backwards through fragments, it is needed to add the transaction to back stack by using the addToBackStack method. It takes a name parameter or null if we do not want to assign a name.
Finally, we schedule a transaction by executing commit(). This is a not momental operation. It schedules operations on the application's main thread. When the main thread is ready, the transaction will be executed. Think about it when planning and implementing your code!