Fragments in Android | Kotlin | Think Like a Child

Affan shaikhsurab
3 min readApr 24, 2024

Imagine you are a child who loves to play with toys. All of your toys are kept in a box, but you have a helper uncle named “Bob” who gives you toys from the box. You say to Uncle Bob, “Can you give me that toy?” and he gives it to you. Later, if you get bored with that toy, you can say to Uncle Bob, “Replace this toy with that toy,” and he will do it for you. Congratulations, you have just learned the basics of fragments!

But what exactly are fragments? In Android development, a fragment is like a toy that you keep in a box, which represents the activity. Uncle Bob is the fragment manager because he manages the fragments for you. If you want a fragment, you simply ask Uncle Bob to give it to you or replace it for you.

Why use fragments? Imagine if you had different toys in different boxes — wouldn’t that take up more space and be harder to manage? Keeping all your toys in one box makes it easier to handle them.

To implement fragments in your Android app, follow these steps:

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

Step 1: Buy the toys

As the first thing you do is to buy toys here the first thing you do is to create fragments show as below

Step 2: Add them to the box

Once you have created the fragments, you need to add them to the activity. To do this, use the “supportFragmentManager” and the “add” method.

So we say uncle bob “supportFragmentManager” to “add” the toys “fragments” in the box “activity”

supportFragmentManager.commit {
//here the commits make sure that this operation happens successfully
setReorderingAllowed(true)
// this make sure of smoothness of the trasition between fragments
add(R.id.example_69 , example_fragment_69::class.java , null)
// here we are saying to add our toy "exmaple_fragment_69" into our box
// and here null -> refers to we are not passing any arguments as a bundle
}

and that’s it !!! .

but wait for a second it’s support fragment manager why support?well, the fragment manager only supports API level 11 and this fragment manager supports all API levels so……… it’s supportFragmentManager got it !!!

Step 3. replace the toy and play !!

so let’s say you added a few more toys in your box and you are playing with one after some time you want to change your toy so you say to bob uncle to replace it for you with a toy Let’s say “superman” so bob uncle will do it for you.

so you say bob uncle “support fragment manager “ to “replace” the current toy “current fragment” with another toy “newer fragment”

supportFragmentManager.commit {
// here we are replacing the current toy in activity_main with example_fragament
replace(R.id.activity_main , example_fragment_80())
setReorderingAllowed(true)
add(R.id.example_69 , example_fragment_69::class.java , null)
}

So, to summarize, Fragments in Android are like toys that you keep in a box. You have a Fragment Manager to help you manage them. You can add fragments to your app’s user interface using the add method and replace them with another fragment using the replace method. And that's it! Congrats, you've learned the basics of Fragments. For more information, check out the Android documentation. Happy coding! and be curious.

--

--