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:
- Create a new instance of the
ViewModelif needed, and - Will reuse an existing instance of the
ViewModelif an activity or fragment was destroyed and recreated as part of a configuration change and is now trying to get theViewModelagain
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.