Junior — Middle
Have you used fragment operations for managing the user interface?
sobes.tech AI
Answer from AI
Yes, fragment transactions are widely used for managing the user interface in Android. Fragments allow creating modular and reusable UI components that can be dynamically added, replaced, or removed within activities.
Main fragment operations:
- Adding (
add) — inserting a new fragment into a container. - Replacing (
replace) — replacing the current fragment with another. - Removing (
remove) — removing a fragment. - Adding to back stack — so the user can navigate back.
Example of adding a fragment:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.container, new MyFragment());
transaction.addToBackStack(null);
transaction.commit();
This allows flexible UI management, such as displaying different screens within a single activity screen.