androidx.activity

Interfaces

FullyDrawnReporterOwner

A class that has a FullyDrawnReporter that allows you to have separate parts of the UI independently register when they have been fully loaded.

OnBackPressedDispatcherOwner

A class that has an OnBackPressedDispatcher that allows you to register a OnBackPressedCallback for handling the system back button.

Classes

BackEventCompat

Compat around the BackEvent class

ComponentActivity

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

ComponentDialog

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

FullyDrawnReporter

Manages when to call Activity.reportFullyDrawn.

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.

SystemBarStyle

The style for the status bar or the navigation bar used in enableEdgeToEdge.

Extension functions summary

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

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

Unit
ComponentActivity.enableEdgeToEdge(
    statusBarStyle: SystemBarStyle,
    navigationBarStyle: SystemBarStyle
)

Enables the edge-to-edge display for this ComponentActivity.

FullyDrawnReporterOwner?

Retrieve the FullyDrawnReporterOwner associated with the given View.

OnBackPressedDispatcherOwner?

Retrieve the OnBackPressedDispatcherOwner associated with the given View.

suspend inline Unit
FullyDrawnReporter.reportWhenComplete(reporter: suspend () -> Unit)

Tells the FullyDrawnReporter to wait until reporter has completed before calling Activity.reportFullyDrawn.

Unit

Set the FullyDrawnReporterOwner associated with the given View.

Unit

Set the OnBackPressedDispatcherOwner associated with the given View.

suspend Unit

Sets the View that will be used as reference to set the PictureInPictureParams.Builder.setSourceRectHint.

inline Lazy<VM>
@MainThread
<VM : ViewModel> ComponentActivity.viewModels(
    noinline extrasProducer: (() -> CreationExtras)?,
    noinline factoryProducer: (() -> ViewModelProvider.Factory)?
)

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.

enableEdgeToEdge

fun ComponentActivity.enableEdgeToEdge(
    statusBarStyle: SystemBarStyle = SystemBarStyle.auto(Color.TRANSPARENT, Color.TRANSPARENT),
    navigationBarStyle: SystemBarStyle = SystemBarStyle.auto(DefaultLightScrim, DefaultDarkScrim)
): Unit

Enables the edge-to-edge display for this ComponentActivity.

To set it up with the default style, call this method in your Activity's onCreate method:

    override fun onCreate(savedInstanceState: Bundle?) {
enableEdgeToEdge()
super.onCreate(savedInstanceState)
...
}

The default style configures the system bars with a transparent background when contrast can be enforced by the system (API 29 or above). On older platforms (which only have 3-button/2-button navigation modes), an equivalent scrim is applied to ensure contrast with the system bars.

See SystemBarStyle for more customization options.

Parameters
statusBarStyle: SystemBarStyle = SystemBarStyle.auto(Color.TRANSPARENT, Color.TRANSPARENT)

The SystemBarStyle for the status bar.

navigationBarStyle: SystemBarStyle = SystemBarStyle.auto(DefaultLightScrim, DefaultDarkScrim)

The SystemBarStyle for the navigation bar.

findViewTreeFullyDrawnReporterOwner

fun View.findViewTreeFullyDrawnReporterOwner(): FullyDrawnReporterOwner?

Retrieve the FullyDrawnReporterOwner associated with the given View. This may be used to indicate that a part of the UI is drawn and ready for first user interaction.

Returns
FullyDrawnReporterOwner?

The FullyDrawnReporterOwner associated with this view and/or some subset of its ancestors

findViewTreeOnBackPressedDispatcherOwner

fun View.findViewTreeOnBackPressedDispatcherOwner(): OnBackPressedDispatcherOwner?

Retrieve the OnBackPressedDispatcherOwner associated with the given View. This may be used to add a callback for the system back button.

Returns
OnBackPressedDispatcherOwner?

The OnBackPressedDispatcherOwner associated with this view and/or some subset of its ancestors

reportWhenComplete

suspend inline fun FullyDrawnReporter.reportWhenComplete(reporter: suspend () -> Unit): Unit

Tells the FullyDrawnReporter to wait until reporter has completed before calling Activity.reportFullyDrawn.

setViewTreeFullyDrawnReporterOwner

fun View.setViewTreeFullyDrawnReporterOwner(
    fullyDrawnReporterOwner: FullyDrawnReporterOwner
): Unit

Set the FullyDrawnReporterOwner associated with the given View. Calls to findViewTreeFullyDrawnReporterOwner from this View or descendants will return fullyDrawnReporterOwner.

This should only be called by constructs such as activities that manage a view tree and handle the dispatch of ComponentActivity.reportFullyDrawn.

Parameters
fullyDrawnReporterOwner: FullyDrawnReporterOwner

FullyDrawnReporterOwner associated with the View

setViewTreeOnBackPressedDispatcherOwner

fun View.setViewTreeOnBackPressedDispatcherOwner(
    onBackPressedDispatcherOwner: OnBackPressedDispatcherOwner
): Unit

Set the OnBackPressedDispatcherOwner associated with the given View. Calls to findViewTreeOnBackPressedDispatcherOwner from this view or descendants will return onBackPressedDispatcherOwner.

This should only be called by constructs such as activities or dialogs that manage a view tree and handle the dispatch of the system back button. Callers should only set a OnBackPressedDispatcherOwner that will be stable.

Parameters
onBackPressedDispatcherOwner: OnBackPressedDispatcherOwner

OnBackPressedDispatcherOwner associated with the View

trackPipAnimationHintView

@RequiresApi(value = 26)
suspend fun Activity.trackPipAnimationHintView(view: View): Unit

Sets the View that will be used as reference to set the PictureInPictureParams.Builder.setSourceRectHint.

Anytime the view position changes, Activity.setPictureInPictureParams will be called with the updated view's position in window coordinates as the PictureInPictureParams.Builder.setSourceRectHint.

Parameters
view: View

the view to use as the reference for the source rect hint

@MainThread
inline fun <VM : ViewModel> ComponentActivity.viewModels(
    noinline extrasProducer: (() -> CreationExtras)? = null,
    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.