The Android Developer Challenge is back! Submit your idea before December 2.

androidx.activity

Classes

ComponentActivity

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

OnBackPressedCallback

Class for handling OnBackPressedDispatcher#onBackPressed() callbacks without strongly coupling that implementation to a subclass of ComponentActivity.

OnBackPressedDispatcher

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

Extension functions summary

For OnBackPressedDispatcher
OnBackPressedCallback
OnBackPressedDispatcher.addCallback(owner: LifecycleOwner? = null, enabled: Boolean = true, onBackPressed: OnBackPressedCallback.() -> Unit)

Create and add a new OnBackPressedCallback that calls onBackPressed in OnBackPressedCallback.handleOnBackPressed.

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

addCallback

fun OnBackPressedDispatcher.addCallback(owner: LifecycleOwner? = null, enabled: Boolean = true, onBackPressed: OnBackPressedCallback.() -> Unit): OnBackPressedCallback

Create and add a new OnBackPressedCallback that calls onBackPressed in OnBackPressedCallback.handleOnBackPressed.

If an owner is specified, the callback will only be added when the Lifecycle is androidx.lifecycle.Lifecycle.State.STARTED.

A default enabled state can be supplied.

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.