Share
Explore

Fragments in Android are like a part of a user interface in an Android Activity.

Think of them as a modular section of an interface, like a "mini-activity" that you can reuse in different activities. Fragments have their own lifecycle, receive their own input events, and you can add or remove them while the activity is running.
Fragments are useful because they allow you to modularize your code, reuse components across different activities, and create dynamic and multi-pane UIs on large screens, such as tablets.
Here's a simple example of how to create a Fragment in Kotlin:
Define a Fragment class:
This is a basic fragment displaying a simple TextView.

class MyFragment : Fragment()
{
override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View?
{ // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_my, container, false) } }
In the above code, MyFragment is a subclass of Fragment.
The onCreateView method is where you inflate the layout for the fragment. R.layout.fragment_my refers to the XML layout file for the fragment.
Create a layout for the Fragment:
This is the XML layout for the Fragment. It's a simple layout with just a TextView.
xmlCopy code
<!-- fragment_my.xml --> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:text="Hello from the Fragment!" />
Add the Fragment to an Activity:
You can dynamically add a Fragment to an Activity's layout.

class MyActivity : AppCompatActivity()

{ override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_my)

// Check that the activity is using the layout version with

// the fragment_container FrameLayout if (findViewById<FrameLayout>

(R.id.fragment_container) != null) {

// However, if we're being restored from a previous state,

// then we don't need to do anything and should return or else

// we could end up with overlapping fragments. if (savedInstanceState != null) { return }

// Create an instance of ExampleFragment val firstFragment = MyFragment()

// Add the fragment to the 'fragment_container' FrameLayout

supportFragmentManager.beginTransaction() .add(R.id.fragment_container, firstFragment).commit() } } }
In this activity, you check if the fragment_container (a FrameLayout) is present.
If yes, you create an instance of MyFragment and add it to the activity using a FragmentTransaction.
Activity's layout (activity_my.xml):
This layout includes a FrameLayout where the fragment will be placed.

<!-- activity_my.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" />
In this example, MyFragment is a basic fragment showing how fragments can be created and added to an activity.
This modular approach is particularly useful for adapting your app's UI flow to various screen sizes and orientations.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.