Home > Enterprise >  Fragment not getting added with add from fragmentManager
Fragment not getting added with add from fragmentManager

Time:03-05

I'm trying to follow android's guide to creating a fragment programmatically but I find that the fragment isn't getting added and that it's onViewCreated method is never called.

I'm not getting any error or crash, the fragment just doesn't appear. I have been on this for more than a day so any help is appreciated.

Here's the code from my MainActivity.kt :

class MainActivity : AppCompatActivity(R.layout.main_activity) {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    if (savedInstanceState == null) {
        supportFragmentManager.commit {
            setReorderingAllowed(true)
            add<MessagesFragment>(R.id.fragment_container_view)
        }
    }
}

}

And here's the main_activity.xml :

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

        <androidx.fragment.app.FragmentContainerView
            android:id="@ id/fragment_container_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

</layout>

Here's the fragment I'm trying to add MessagesFragment.kt :

class MessagesFragment : Fragment() {

    private lateinit var binding : MessagesFragmentBinding
    private val messagesViewModel: MessagesViewModel by viewModels()
    private val messagesAdapter = MessagesAdapter()

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setupView()
        setupRecycler()
    }

    private fun setupRecycler() {
        binding.messageListRecyclerView.adapter = messagesAdapter

        postponeEnterTransition()
        view?.doOnPreDraw { startPostponedEnterTransition() }
    }

    private fun setupView() {
        messagesViewModel.state.onEach {
            messagesAdapter.submitList(it.messages)
        }
    }
}

And it's associated messages_fragment.xml :

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

    <androidx.recyclerview.widget.RecyclerView
        android:id="@ id/messageListRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        tools:listitem="@layout/message_item" />

</FrameLayout>

CodePudding user response:

You never have your fragment actually inflate the messages_fragment.xml file.

You'll want to change your fragment to call the Fragment constructor that takes a LayoutRes:

class MessagesFragment : Fragment(R.layout.messages_fragment) {

That will ensure that your view is inflated - which is a prerequisite to onViewCreated() being called.

  • Related