Save the date! Android Dev Summit is coming to Sunnyvale, CA on Oct 23-24, 2019.

androidx.activity

Classes

ComponentActivity

Base class for activities that enables composition of higher level components.

OnBackPressedDispatcher

Dispatcher that can be used to register OnBackPressedCallback instances for handling the ComponentActivity#onBackPressed() callback via composition.

Extension functions summary

For ComponentActivity
Lazy<VM>
ComponentActivity.viewModels(noinline factoryProducer: () -> ViewModelProvider.Factory = null)

Returns a Lazy delegate to access the ComponentActivity's ViewModel, if factoryProducer is specified then ViewModelProvider.Factory returned by it will be used to create ViewModel first time.

Extension functions

viewModels

@MainThread inline fun <reified VM : ViewModel> ComponentActivity.viewModels(noinline factoryProducer: () -> ViewModelProvider.Factory = null): Lazy<VM>

Returns a Lazy delegate to access the ComponentActivity's ViewModel, if factoryProducer is specified then ViewModelProvider.Factory returned by it will be used to create ViewModel first time.


class MyComponentActivity : ComponentActivity() {
    val viewmodel: MyViewModel by viewmodels()
}

This property can be accessed only after the Activity is attached to the Application, and access prior to that will result in IllegalArgumentException.