Junior
What is the difference between FragmentManager and fragment transactions?
sobes.tech AI
Answer from AI
FragmentManager manages:
- The lifecycle of fragments.
- The back stack (
Back Stack). - Searching for fragments by id or tag.
Fragment transactions (FragmentTransaction) are used to perform operations on fragments, such as:
- Adding (
add()) - Removing (
remove()) - Replacing (
replace()) - Showing (
show()) - Hiding (
hide())
Example:
// Get the FragmentManager
FragmentManager fragmentManager = getSupportFragmentManager();
// Begin a transaction
FragmentTransaction transaction = fragmentManager.beginTransaction();
// Perform an operation (e.g., replace one fragment with another)
MyFragment newFragment = new MyFragment();
transaction.replace(R.id.fragment_container, newFragment);
// Add the transaction to the back stack (optional)
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();