The second Android 11 Developer Preview is now available, test it out and share your feedback.

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 androidx.activity.result.ActivityResultCaller
() -> Unit
ActivityResultCaller.prepareCall(contract: ActivityResultContract<I, O>, input: I, registry: ActivityResultRegistry, crossinline callback: (O) -> Unit)

A version of ActivityResultCaller.prepareCall that additionally takes an input right away, producing a launcher that doesn't take any additional input when called.

() -> Unit
ActivityResultCaller.prepareCall(contract: ActivityResultContract<I, O>, input: I, crossinline callback: (O) -> Unit)

A version of ActivityResultCaller.prepareCall that additionally takes an input right away, producing a launcher that doesn't take any additional input when called.

For androidx.activity.result.ActivityResultLauncher
operator Unit
ActivityResultLauncher<I>.invoke(input: I, options: ActivityOptionsCompat? = null)

Convenience method to launch a prepared call using an invoke operator.

operator Unit

Convenience method to launch a no-argument prepared call using an invoke operator without arguments.

operator Unit

Convenience method to launch a no-argument prepared call using an invoke operator without arguments.

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.

invoke

operator fun <I> ActivityResultLauncher<I>.invoke(
    input: I,
    options: ActivityOptionsCompat? = null
): Unit

Convenience method to launch a prepared call using an invoke operator.

invoke

operator fun ActivityResultLauncher<Void?>.invoke(options: ActivityOptionsCompat? = null): Unit

Convenience method to launch a no-argument prepared call using an invoke operator without arguments.

invoke

@JvmName("invokeUnit") operator fun ActivityResultLauncher<Unit>.invoke(options: ActivityOptionsCompat? = null): Unit

Convenience method to launch a no-argument prepared call using an invoke operator without arguments.

prepareCall

inline fun <I, O> ActivityResultCaller.prepareCall(
    contract: ActivityResultContract<I, O>,
    input: I,
    registry: ActivityResultRegistry,
    crossinline callback: (O) -> Unit
): () -> Unit

A version of ActivityResultCaller.prepareCall that additionally takes an input right away, producing a launcher that doesn't take any additional input when called.

prepareCall

inline fun <I, O> ActivityResultCaller.prepareCall(
    contract: ActivityResultContract<I, O>,
    input: I,
    crossinline callback: (O) -> Unit
): () -> Unit

A version of ActivityResultCaller.prepareCall that additionally takes an input right away, producing a launcher that doesn't take any additional input when called.

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.