37.3 Adding a Fragment to an Activity using the Layout XML File
Fragments may be incorporated into an activity either by writing Kotlin code or by embedding the fragment into the activity’s XML layout file. Regardless of the approach used, a key point to be aware of is that when the support library is being used for compatibility with older Android releases, any activities using fragments must be implemented as a subclass of FragmentActivity instead of the AppCompatActivity class:
package com.example.myFragmentDemo
import androidx.fragment.app.FragmentActivity
import android.os.Bundle
class MainActivity : FragmentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
Fragments are embedded into activity...