30.3 Adding a Fragment to an Activity using the Layout XML File
Fragments may be incorporated into an activity either by writing Java 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 android.os.Bundle;
import androidx.fragment.app.FragmentActivity;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
.
.
Fragments are embedded into activity layout files using the FragmentContainerView class. The following example layout embeds the fragment created in the previous section of this chapter into an activity layout:
<?xml version="1.0" encoding="utf-8"...