Step #4: Injecting the Motor

Now, we can have RosterListFragment use the RosterMotor.

Add a new motor property:

  private val motor: RosterMotor by viewModel()

viewModel() is another Koin extension function, one specifically designed to get AndroidX ViewModel objects from Koin:

import org.koin.androidx.viewmodel.ext.android.viewModel

In particular, viewModel() will:

Our code does not care which of those scenarios occurs. We know that motor will give us our RosterMotor, and whether it is a brand-new RosterMotor or an existing one from a previous RosterListFragment does not matter.

We will start using motor, and the associated ToDoRepository, in the next tutorial, when we show our list of to-do items on the screen.


Prev Table of Contents Next

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