Sobes.tech
Junior

How can fragments be implemented in an Activity?

sobes.tech AI

Answer from AI

Fragments in an Activity are used to build the user interface. Fragment management is done through FragmentManager.

Main ways to add a fragment to an Activity:

  1. Statically in XML layout:
    The fragment is declared directly in the Activity layout file. This is the simplest way, but less flexible, as the fragment is added when the Activity is created and cannot be easily replaced or removed dynamically.

    <!-- activity_main.xml -->
    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <fragment
            android:id="@+id/my_fragment"
            android:name="com.example.yourapp.MyFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </FrameLayout>
    
  2. Dynamically using FragmentManager:
    Fragments are added, removed, or replaced programmatically during the execution of the Activity. This provides greater flexibility and allows creating dynamic interfaces, for example, adapted to screen size or application state. Management of dynamic fragment operations is done through FragmentTransaction.

    // Inside Activity
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        if (savedInstanceState == null) {
            // Create a new fragment instance
            MyFragment firstFragment = new MyFragment();
    
            // Get FragmentManager
            FragmentManager fragmentManager = getSupportFragmentManager();
    
            // Begin transaction
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    
            // Add fragment to container
            // R.id.fragment_container - ID of FrameLayout or other View
            fragmentTransaction.add(R.id.fragment_container, firstFragment);
    
            // Optionally: add transaction to Back Stack for return capability
            fragmentTransaction.addToBackStack(null);
    
            // Commit transaction
            fragmentTransaction.commit();
        }
    }
    

Main FragmentTransaction operations:

Operation Description
add() Adds a fragment to the container.
replace() Replaces the fragment in the container with another fragment.
remove() Removes a fragment from the container.
hide() Hides a fragment (its View becomes invisible), but it remains attached.
show() Shows a previously hidden fragment.
detach() Detaches a fragment from its View but preserves its state.
attach() Reattaches a previously detached fragment, creating its View.
addToBackStack() Adds the transaction to the Back Stack, allowing the user to return to the previous fragment state.
commit() Applies the transaction asynchronously.
commitNow() Applies the transaction synchronously.
How can fragments be implemented in an Activity… - sobes.tech