In this tutorial you will learn about the Kotlin Fragment and its application with practical example.
Fragment is a part of Activity or also we can say sub activity. It represent multiple screen inside an activity.
The fragment’s lifecycle is affected by the host activity’s lifecycle, For example if host activity is in pause status so all fragment will be in pause status and if activity is destroyed so all fragment will be destroyed. It contains callback methods like an activity, like onCreate(), onStart(), onPause() and onStop().
It has been already discussed in Android Tutorial here we are going to create Fragment with Kotlin in Android.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.w3adda.myapplication import android.content.Context import android.net.Uri import android.os.Bundle import android.support.v4.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup class KotlinFragment : Fragment(){ private var kotlinFragment: KotlinFragment? = null override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { var view:View?=null view=inflater!!.inflate(R.layout.fragment_kotlin,container,false) return view } } |
So it was the way from which we can create fragment in Android with Kotlin And if you want more details about Fragment so please visit Android Fragment Article.