Parent fragment's onCreateView is called after child fragments onCreateView

726 Views Asked by At

I have a ParentFragment and a ChildFragment that extends from it. I placed a log in onCreateView method of both fragments. But ParentFragment's onCreateView is being called last.

However, the same parent-child relationship between activities is producing the expected result. Parent activities onCreate is being called first and then child activity's onCreate.

ParentFragment

abstract class ParentFragment<T: ViewDataBinding>: Fragment(){

    @LayoutRes
    abstract fun getLayoutResId(): Int

    protected lateinit var binding: T


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        Log.d("-----------", "onCreateView ParentFragment")
        return DataBindingUtil.inflate<T>(inflater, getLayoutResId(), container, false).apply { binding = this }.root
    }

ChildFragment

class ChildFragment: ParentFragment<FragmentChildBinding>() {

    @LayoutRes
    override fun getLayoutResId() = R.layout.fragment_child

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        Log.d("-----------", "onCreateView ChildFragment")

        return inflater.inflate(R.layout.fragment_child, container, false)
    }

Result for Fragments:

2019-12-10 10:54:28.054 32450-32450/blabla.yeahyeah D/-----------: onCreateView ChildFragment
2019-12-10 10:54:28.054 32450-32450/blabla.yeahyeah D/-----------: onCreateView ParentFragment

Result for Activities:

2019-12-10 10:54:27.796 32450-32450/blabla.yeahyeah D/-----------: onCreate ParentActivity
2019-12-10 10:54:27.931 32450-32450/blabla.yeahyeah D/-----------: onCreate ChildActivity

Can you please help with this? Am I missing something or is it an expected behavior?

The reason why I am overriding onCreateView is that I am trying to connect my viewModel to my binding in ChildFragment like this:

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        Log.d("-----------", "onCreateView ChildFragment")

        val viewModel = getViewModel()
        binding.viewModel = viewModel //CRASH HAPPENING HERE. THIS VARIABLE IS NOT INITIALIZED
        binding.lifecycleOwner = this

        return binding.root
    }

Currently, I am getting crash becuse binding variable is not initialized when I access it from ChildFragment. Since this variable needs to be initialized in ParentFragment and ParentFragment is being called after ChildFragment, I amgetting crash as I understood.

0

There are 0 best solutions below