Step #11: Displaying the Layout

In DisplayFragment, add a binding field, pointing to our newly-generated TodoDisplayBinding class from our todo_display layout resource:

  private var binding: TodoDisplayBinding? = null

Then, in DisplayFragment, add an onCreateView() function:

  override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
  ) = TodoDisplayBinding.inflate(inflater, container, false)
    .apply { binding = this }
    .root

This works akin to how onCreateViewHolder() does in RosterAdapter, inflating the binding from the resource, using the code-generated TodoDisplayBinding class. Here, we assign the binding itself to the binding property, while returning the root View of the inflated layout.

Also, add this onDestroyView() function to DisplayFragment:

  override fun onDestroyView() {
    binding = null

    super.onDestroyView()
  }

As with RosterListFragment, this sets binding back to null, so we do not leak the binding after our fragment’s UI is destroyed.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.