Static and Dynamic Fragments with code example - devshafique/Native-Android-Development-Blogs GitHub Wiki

In Android, a fragment is a reusable section of an activity that can have its own user interface and lifecycle. Fragments can be used to represent different parts of an interface and can be combined with other fragments in a single activity to create a multi-pane UI. This allows for more modular and reusable code, as well as the ability to easily adapt the UI for different screen sizes and orientations.

There are several different types of fragments in Android, each with their own specific use cases and functionality.

Static Fragments:

Static fragments are a type of fragment in Android development that are not associated with a specific activity or UI element. They are typically used to represent a portion of an interface that will remain static and unchanged throughout the lifecycle of the app. They are useful for creating reusable UI elements, such as header or footer sections, that can be included in multiple activities or layouts. Static fragments are defined in XML layout files and can be added to an activity or other UI element using the tag.

These are fragments that are defined and added to an activity during the development of the application and are not expected to change during runtime. They can be added to an activity layout using XML.

Here's an example of how to create and use a static fragment in Android:

  1. First, you need to create a new layout file for the fragment. In this example, the layout file is called fragment_example.xml and it contains a TextView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello, this is a static fragment" />

</LinearLayout>
  1. Next, you need to create a new class for the fragment. In this example, the class is called ExampleFragment and it extends the Fragment class:
public class ExampleFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_example, container, false);
    }
}
  1. Now you can add the fragment to an activity layout using XML. In this example, the activity layout file is called activity_example.xml and it contains a FrameLayout where the fragment will be added:
<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">

</FrameLayout>
  1. Next, you need to add the fragment to the activity in the onCreate() method of the activity.
public class ExampleActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_example);

        // Add the fragment to the activity
        ExampleFragment exampleFragment = new ExampleFragment();
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, exampleFragment)
                .commit();
    }
}

This is a basic example of how to create a static fragment in Android. You can also pass data or arguments to the fragment by creating a Bundle object, setting the data in the Bundle, and passing it to the fragment when it is added to the activity.

Dynamic Fragments

Dynamic fragments in Android are fragments that can be added, removed or replaced at runtime, allowing for more flexibility and dynamic changes in the user interface of an application. They are added to an activity using the FragmentManager and FragmentTransaction, which can be useful for creating multi-pane layouts on larger devices, and changing the interface based on user input or other conditions. They are in contrast to Static fragments that are defined and added during the development of the application and are not expected to change during runtime.

Here's an example of how to create and use a dynamic fragment in Android:

  1. First, you need to create a new layout file for the fragment. In this example, the layout file is called fragment_example.xml and it contains a TextView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello, this is a dynamic fragment" />

</LinearLayout>
  1. Next, you need to create a new class for the fragment. In this example, the class is called ExampleFragment and it extends the Fragment class:
public class ExampleFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_example, container, false);
    }
}
  1. Next, you need to create an instance of the fragment in the activity and then add it to the activity using the FragmentManager and FragmentTransaction.
public class ExampleActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_example);

        // Create a new instance of the fragment
        ExampleFragment exampleFragment = new ExampleFragment();

        // Add the fragment to the activity
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.fragment_container, exampleFragment);
        fragmentTransaction.commit();
    }
}
  1. You can also use replace() method instead of add() method to replace the existing fragment by a new one.
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, newFragment);
fragmentTransaction.commit();

This is a basic example of how to create a dynamic fragment in Android. You can also pass data or arguments to the fragment by creating a Bundle object, setting the data in the Bundle, and passing it to the fragment when it is added to the activity.

You can also use the FragmentTransaction to add, remove, and replace fragments in the activity, as well as perform other actions such as animation, and also addToBackStack() method to handle back button.

⚠️ **GitHub.com Fallback** ⚠️