Extensions to get ViewModel
s, use LiveData
and observe Lifecycle
s.
This makes using Android Architecture Components nicer in Kotlin.
This is a GenericLifecycleObserver
sub-interface that has lifecycle state
change methods (like onResume(…)
or onPause(…)
) with default
implementations so you override only the ones you need.
ViewModels are instantiated by host Activity
scope.
The activityScope<YourViewModel>()
extensions on FragmentActivity
and
support Fragment
return a Lazy<YourViewModel
instance that you can
use on a delegated property in your Activity
or Fragment
:
class YourActivity : AppCompatActivity() {
private val viewModel: YourViewModel by activityScope()
private val anotherViewModel by activityScope<AnotherViewModel>()
}
class SomeFragment : Fragment() {
private val viewModel by activityScope<YourViewModel>()
}
class YourActivity : AppCompatActivity() {
private val viewModel by activityScope<YourViewModel>()
override fun onCreate(savedInstanceState: Bundle?) {
observe(viewModel.yourLiveData) { data: YourData? ->
updateUi(data)
}
observeNotNull(viewModel.anotherLiveData) {
doSomething(it.someProperty)
doSomethingElse(it)
}
}
}
class YourViewModel : ViewModel() {
val yourLiveData: LiveData<YourData> = createYourLiveData()
val anotherLiveData = yourLiveData.map { it?.someProperty }
}
Note that the map
lambda runs on UI thread, so very light operations like
getting a property is right, but long/blocking operations are not (would
result in lags or ANRs).
implementation("com.louiscad.splitties:splitties-arch-lifecycle:$splitties_version")