Android Fragments

In Android, there are different types of devices having different sizes and different hardware designs. To introduce modularity and reusability of the activity’s UI, Fragments were introduced.

Fragments are called as reusable UI components who have their own layout, lifecycle and can handle its own input events. They very similar to how Activity functions in Android. Major difference is that Fragment provides the required flexibility to update the UI design in an easier way. Unlike Activity, Fragments can easily be added, removed and can easily enter or exit provided screen space.

Let's have an example News Application. Assume the app has 2 screens:
1. List of News items

2. Details of each new item (open when an news items is clicked in #1 screen)

Generally, having 2 activities for each of these screens will be the first possible approach. For phone devices (where width is smaller) this approach can be considered and implemented. But for the devices with larger width having this approach will result in unnecessary wastage of available space. For such devices both #1 and #2 screens can be shown side-to-side thereby making it easier for users to operate the application.

But it cannot be achieved using 2 different activities and one should search for some UI components which are modular enough to be used in both these scenarios. For this very reason Fragments are used. So the expected design will be to build both these screens using Fragments.

At the higher level - When both Activity-Fragments are used the design would be to keep the support required to manage navigation of these screens (fragments) in Activity and the content part will be moved to Fragments.

To summarise:

  • Fragments are reusable UI portions of app’s UI
  • Fragments can handle its own layout, inputs events and lifecycle.
  • Fragments helps in making app modular
  • Fragments help in managing different screen sized devices easily and efficiently
  • Fragments need to be hosted by Activity or another Fragment. Cannot live alone.

How should be the Fragment designed?

  • Should be modular and act as an independent component.
  • Should not be dependent on other app components
  • Try have it handle very specific functionality of the app (avoid adding a lot of functionalities in single fragment)
  • Try to keep it light weight

How to add a Fragment?

Steps to add a fragment in the project (Via XML)

  • Create a new fragment class and extend it with AndroidX Fragment class.
  • Create a new XML or update existing to add newly created Fragment.
  • Make sure to use FragmentContainerView to add the Fragment. Set the fragment using android:name or android:class attribute
  • Now when activity’s layout is inflated, a specified fragment is created and added to FragmentManager of the activity.

Example

<androidx.fragment.app.FragmentContainerView

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/fragment_container_view"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:name="com.example.FirstFragment" />


Steps to add a fragment in the project (Via code dynamically)

  • Create a new fragment class and extend it with AndroidX Fragment class.
  • Create a new XML or update existing to add newly created Fragment.
  • Make sure to use FragmentContainerView to add the Fragment. Do not set android:name or android:class attribute.
  • Create FragmentTransaction object in the activity using the fragment can be added to the FragmentManager of the activity.
Example

class FirstActivity : AppCompatActivity(R.layout.activity) {

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

        if (savedInstanceState == null) {

            supportFragmentManager.commit {

                setReorderingAllowed(true)

                add<FirstFragment>(R.id.fragment_container_view)

            }

        }

    }

}


Upcoming post will be about Fragment Lifecycle.


Resources

https://developer.android.com/guide/fragments/create

Comments

Popular posts from this blog

Fragment Lifecycle

AndroidManifest

Activity Lifecycle