Android Fragment Example in Kotlin

Android Fragment is part of Android app UI or represents a behavior (DialogFragment,ListFragment)of the particular portion in an application. You can think Fragments as module section UI or behavior of Activity with its own Lifecycle. This tutorial you will see Android Fragment Example in Kotlin.

Come to the definition, You can use multiple fragments in a activity and same fragment in multiple Activity in the Android application.

Android Fragment example tutorial Android studio

Fragments lifecycle is directly affected by Activity. For example, when the activity is paused, so are all fragments in it, and when the activity is destroyed, so are all fragments. You can add and remove fragments in activity during activity onResume() state (while an activity is running ).

Android introduced fragments in Android 3.0 (API level 11) to support more dynamic and flexible UI on a large screen like tablets.

You should design each fragment as a modular and reusable activity component because each fragment has its own layout, behavior and lifecycle callbacks.

Android Fragment example tutorial Kotlin Android studio

Kotlin is known the official language for android app development, so we will develop  Android Fragment example app using Kotlin.

 

Creating a Fragment

To create a fragment, you must create a subclass of Fragment (or an existing subclass of it).

You can do it with 2 ways, First Create a class and extended with Fragment class and create resource layout file.

OR

Second ways is Right Click on Package -> New -> Fragment -> Fragments (Blank)

then give Fragment Name , check Fragment Layout Name – > Finish 

Android fragment example create

That’s it your fragments has created. I removed (unchecked) factory methods and interface callbacks.

 

Completed Code of Android Fragment Example

Step 1. Create an android project in the android studio (Follow this tutorial: Android First Program in Android Studio)

Step 2. Create 2 Fragments and its resource layout. Follow upper section #Creating a Fragment 

First fragment resource file fragment_first.xml, Outer Layout is FrameLayout, you can use the choice of your layout (LinearLayout, RelativeLayout or ConstraintLayout etc)

 

FirstFragment.kotlin

 

Second fragment resource file fragment_second.xml, Both fragments layout containing TextView widget to show text.

 

SecondFragment.kotlin

 

Step 3. Add following code in the main_activity.xml resource file

Its Static way to add fragment in Activity so you must add android:name="in.eyehunt.androidfragmentexample.SecondFragment" and android:name="in.eyehunt.androidfragmentexample.FirstFragment  code

 

MainActivity.kotlin will remain same, see below code

Step 4. Now Run the application, in emulator or On your Android device

Output screenshot Android Fragment example app :

 

Android Fragment example

 

Download Link and Source code Android Fragment example in Github

https://github.com/EyeHunts/AndroidFragmentExample/

 

Note : This example (Project) is developed in Android Studio 3.0.1 ,tested on Android 7.1.1 ( Android Nougat), compile SDK version API 26: Android 8.0 (Oreo)

MinSdkVersion=”15″

TargetSdkVersion=”26″

Coding in Kotlin

Managing Fragments

To manage the fragments in your activity (dynamically), you need to use FragmentManager. To get it, call getSupportFragmentManager() from your activity.

Performing Fragment Transactions

A great feature about using fragments in your activity is the ability to add, remove, replace, and perform other actions with them, in response to user interaction. Each set of changes that you commit to the activity is called a transaction and you can perform one using APIs in FragmentTransaction.

Bonus: Example of Using Managing Fragments and Performing Fragment Transactions ” Creating and Using Fragments dynamically


One thought on “Android Fragment Example in Kotlin

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.