'Fragment Not Showing in Kotlin Android Studio

I am learning Fragments in Kotlin Android Studio. I am unsure why the fragments are not showing.

Issues:

  • No Fragments are shown
  • Fragment One not loaded by default

Here are the relevant codes:

MainActivity.kt

class MainActivity : AppCompatActivity() {

// boolean to know which fragment is currently active
var isFragmentOneLoaded = true
val manager = supportFragmentManager

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val changeFragmentButton = findViewById<Button>(R.id.changeFragmentButton)

    // so by default Fragment One will be loaded
    ShowFragmentOne()
    
    changeFragmentButton.setOnClickListener{
        if (isFragmentOneLoaded)
            ShowFragmentTwo()
        else
            ShowFragmentOne()
    }
}

fun ShowFragmentOne() {
    val transaction =
        manager.beginTransaction()
    val fragment = FragmentOne()
    transaction.replace(R.id.fragment_holder, fragment)
    transaction.addToBackStack(null)
    transaction.commit()
    isFragmentOneLoaded = true
}

fun ShowFragmentTwo() {
    val transaction =
        manager.beginTransaction()
    val fragment = FragmentTwo()
    transaction.replace(R.id.fragment_holder, fragment)
    transaction.addToBackStack(null)
    transaction.commit()
    isFragmentOneLoaded = false
}

}

FragmentOne.kt

class FragmentOne : Fragment(){

    val TAG = "FragmentOne"

    override fun onAttach(context: Context) {
        Log.d(TAG, "onAttach")
        super.onAttach(context)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        Log.d(TAG, "onCreate")
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        //return super.onCreateView(inflater, container, savedInstanceState)
        Log.d(TAG,"onCreateView")
        return inflater!!.inflate(R.layout.fragment_one,container,false) // !! ---> means it will not be null
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        Log.d(TAG, "onActivityCreated")
        super.onActivityCreated(savedInstanceState)
    }

    override fun onStart() {
        Log.d(TAG, "onStart")
        super.onStart()
    }

    override fun onResume() {
        Log.d(TAG, "onResume")
        super.onResume()
    }

    override fun onPause() {
        Log.d(TAG, "onPause")
        super.onPause()
    }

    override fun onStop() {
        Log.d(TAG, "onStop")
        super.onStop()
    }

    override fun onDestroyView() {
        Log.d(TAG, "onDestroyView")
        super.onDestroyView()
    }

    override fun onDestroy() {
        Log.d(TAG, "onDestroy")
        super.onDestroy()
    }

    override fun onDetach() {
        Log.d(TAG, "onDetach")
        super.onDetach()
    }

}

FragmentTwo.kt

class FragmentTwo : Fragment(){

// this TAG will be used to display the log message
val TAG = "FragmentTwo"

... same as Fragment One ...

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <Button
        android:id="@+id/changeFragmentButton"
        android:layout_width="346dp"
        android:layout_height="wrap_content"
        android:layout_margin="30dp"
        android:text="Change Fragment" />

    <FrameLayout
        android:id="@+id/fragment_holder"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

fragment_one.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="93dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="158dp"
        android:layout_marginTop="298dp"
        android:layout_marginEnd="160dp"
        android:layout_marginBottom="414dp"
        android:text="@string/fragment_one" />

</RelativeLayout>

fragment_two.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="93dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="160dp"
        android:layout_marginTop="325dp"
        android:layout_marginEnd="158dp"
        android:layout_marginBottom="387dp"
        android:text="@string/fragment_two" />
</RelativeLayout>

Here's what my logcat shows when I click the button When I first click the button When I click the button again



Solution 1:[1]

Try this code

fun setCurrentFragment(fragment: Fragment) {
        supportFragmentManager.beginTransaction().apply {
            setCustomAnimations(
                R.anim.slide_in,
                R.anim.slide_out,
                R.anim.slide_in,
                R.anim.slide_out
            )
            replace(R.id.fragment_container, fragment)
            commit()
        }

Remove the animations part if you don't want it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abugewa Zenith