androidx.activity
Interfaces
FullyDrawnReporterOwner |
A class that has a |
OnBackPressedDispatcherOwner |
A class that has an |
Classes
BackEventCompat |
Compat around the |
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 |
OnBackPressedCallback |
Class for handling |
OnBackPressedDispatcher |
Dispatcher that can be used to register |
SystemBarStyle |
The style for the status bar or the navigation bar used in |
Objects
ActivityFlags |
A collection of runtime feature flags used to guard behavior changes, migrations, or experimental paths within the |
Annotations
ExperimentalActivityApi |
Marks declarations that are experimental in the Activity APIs. |
Extension functions summary
Extension functions
addCallback
fun OnBackPressedDispatcher.addCallback(
owner: LifecycleOwner? = null,
enabled: Boolean = true,
onBackPressed: OnBackPressedCallback.() -> Unit
): OnBackPressedCallback
Creates and registers a new OnBackPressedCallback that invokes onBackPressed.
If a LifecycleOwner is provided, the callback’s enabled state automatically follows the lifecycle: it is enabled while the lifecycle is at least State.STARTED and disabled otherwise. The callback stays registered until the LifecycleOwner is destroyed.
A default enabled state can be supplied.
Legacy Behavior
To restore the legacy add/remove behavior, set ActivityFlags.isOnBackPressedLifecycleOrderMaintained to false. In legacy mode, the handler is added on Lifecycle.Event.ON_START and removed on Lifecycle.Event.ON_STOP, which may change dispatch ordering across lifecycle transitions.
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 |
navigationBarStyle: SystemBarStyle = SystemBarStyle.auto(DefaultLightScrim, DefaultDarkScrim) |
The |
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 |
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 |
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 |
|
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 |
|
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 |
viewModels
@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.