Fragment instantiation using companion objects

We have to use a companion object to instantiate a fragment. Remember we have to use the newInstance() method to initialize a fragment because we cannot use the parameterized constructor for fragments. Our UserTimeLineFragment and HomeTimeLineFragment uses a companion object to help the fragment instantiation:

    class UserTimeLineFragment : TwitterFragment() {      override fun loadTimeline() {        // Code goes here      }      companion object {        /**          * Use this factory method to create a new instance of          * this fragment using the provided parameters.          *          * @return A new instance of fragment TwitterFragment.          */        fun newInstance(): UserTimeLineFragment =          UserTimeLineFragment()      }    }

Get Kotlin Blueprints now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.