Google is committed to advancing racial equity for Black communities. See how.

ComponentActivity

open class ComponentActivity : ComponentActivity, ContextAware, LifecycleOwner, ViewModelStoreOwner, HasDefaultViewModelProviderFactory, SavedStateRegistryOwner, OnBackPressedDispatcherOwner, ActivityResultRegistryOwner, ActivityResultCaller
androidx.activity.ComponentActivity

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

Rather than all functionality being built directly into this class, only the minimal set of lower level building blocks are included. Higher level components can then be used as needed without enforcing a deep Activity class hierarchy or strong coupling between components.

Summary

Public constructors

Default constructor for ComponentActivity.

<init>(@LayoutRes contentLayoutId: Int)

Alternate constructor that can be used to provide a default layout that will be inflated as part of super.onCreate(savedInstanceState).

Public methods
open Unit
addContentView(view: View!, params: LayoutParams!)

Unit

Add a new OnContextAvailableListener for receiving a callback for when this class is associated with a android.content.Context.

ActivityResultRegistry

Get the ActivityResultRegistry associated with this activity.

open ViewModelProvider.Factory

Returns the default ViewModelProvider.Factory that should be used when no custom Factory is provided to the ViewModelProvider constructors.

open Any?

Return the value previously returned from onRetainCustomNonConfigurationInstance().

open Lifecycle

Returns the Lifecycle of the provider.

OnBackPressedDispatcher

Retrieve the OnBackPressedDispatcher that will be triggered when onBackPressed() is called.

SavedStateRegistry

open ViewModelStore

Returns the ViewModelStore associated with this activity

open Unit

Called when the activity has detected the user's press of the back key.

open Unit
onRequestPermissionsResult(requestCode: Int, @NonNull permissions: Array<String!>, @NonNull grantResults: IntArray)

open Any?

Use this instead of onRetainNonConfigurationInstance().

Any?

Retain all appropriate non-config state.

open Context?

ActivityResultLauncher<I>
registerForActivityResult(@NonNull contract: ActivityResultContract<I, O>, @NonNull registry: ActivityResultRegistry, @NonNull callback: ActivityResultCallback<O>)

ActivityResultLauncher<I>
registerForActivityResult(@NonNull contract: ActivityResultContract<I, O>, @NonNull callback: ActivityResultCallback<O>)

Unit

open Unit
setContentView(@LayoutRes layoutResID: Int)

open Unit

open Unit
setContentView(view: View!, params: LayoutParams!)

open Unit
startActivityForResult(intent: Intent!, requestCode: Int)

open Unit
startActivityForResult(intent: Intent!, requestCode: Int, @Nullable options: Bundle?)

open Unit
startIntentSenderForResult(intent: IntentSender!, requestCode: Int, @Nullable fillInIntent: Intent?, flagsMask: Int, flagsValues: Int, extraFlags: Int)

open Unit
startIntentSenderForResult(intent: IntentSender!, requestCode: Int, @Nullable fillInIntent: Intent?, flagsMask: Int, flagsValues: Int, extraFlags: Int, @Nullable options: Bundle?)

Protected methods
open Unit
onActivityResult(requestCode: Int, resultCode: Int, @Nullable data: Intent?)

open Unit
onCreate(@Nullable savedInstanceState: Bundle?)

If your ComponentActivity is annotated with ContentView, this will call setContentView(int) for you.

open Unit
onSaveInstanceState(@NonNull outState: Bundle)

Extension functions
From androidx.compose.ui.platform
Composition
ComponentActivity.setContent(parent: CompositionReference = Recomposer.current(), content: () -> Unit)

Composes the given composable into the given activity.

From androidx.activity
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.

Public constructors

<init>

ComponentActivity()

Default constructor for ComponentActivity. All Activities must have a default constructor for API 27 and lower devices or when using the default android.app.AppComponentFactory.

<init>

ComponentActivity(@LayoutRes contentLayoutId: Int)

Alternate constructor that can be used to provide a default layout that will be inflated as part of super.onCreate(savedInstanceState).

This should generally be called from your constructor that takes no parameters, as is required for API 27 and lower or when using the default android.app.AppComponentFactory.

Public methods

addContentView

open fun addContentView(
    view: View!,
    params: LayoutParams!
): Unit

addOnContextAvailableListener

fun addOnContextAvailableListener(@NonNull listener: OnContextAvailableListener): Unit

Add a new OnContextAvailableListener for receiving a callback for when this class is associated with a android.content.Context.

Listeners are triggered in the order they are added when added before the Context is available. Listeners added after the context has been made available will have the Context synchronously delivered to them as part of this call. Any listener added here will receive a callback as part of super.onCreate(), but importantly before any other logic is done (including calling through to the framework Activity#onCreate(Bundle) with the exception of restoring the state of the SavedStateRegistry for use in your listener.

Parameters
listener OnContextAvailableListener: The listener that should be added.

getActivityResultRegistry

@NonNull fun getActivityResultRegistry(): ActivityResultRegistry

Get the ActivityResultRegistry associated with this activity.

Return
ActivityResultRegistry the ActivityResultRegistry

getDefaultViewModelProviderFactory

@NonNull open fun getDefaultViewModelProviderFactory(): ViewModelProvider.Factory

Returns the default ViewModelProvider.Factory that should be used when no custom Factory is provided to the ViewModelProvider constructors.

The extras of getIntent() when this is first called will be used as the defaults to any androidx.lifecycle.SavedStateHandle passed to a view model created using this factory.

Return
ViewModelProvider.Factory a ViewModelProvider.Factory

getLastCustomNonConfigurationInstance

@Nullable open fun getLastCustomNonConfigurationInstance(): Any?

Deprecated: Use a androidx.lifecycle.ViewModel to store non config state.

Return the value previously returned from onRetainCustomNonConfigurationInstance().

getLifecycle

@NonNull open fun getLifecycle(): Lifecycle

Returns the Lifecycle of the provider.

Overriding this method is no longer supported and this method will be made final in a future version of ComponentActivity. If you do override this method, you must:

  1. Return an instance of LifecycleRegistry
  2. Lazily initialize your LifecycleRegistry object when this is first called. Note that this method will be called in the super classes' constructor, before any field initialization or object state creation is complete.
Return
Lifecycle The lifecycle of the provider.

getOnBackPressedDispatcher

@NonNull fun getOnBackPressedDispatcher(): OnBackPressedDispatcher

Retrieve the OnBackPressedDispatcher that will be triggered when onBackPressed() is called.

Return
OnBackPressedDispatcher The OnBackPressedDispatcher associated with this ComponentActivity.

getSavedStateRegistry

@NonNull fun getSavedStateRegistry(): SavedStateRegistry

getViewModelStore

@NonNull open fun getViewModelStore(): ViewModelStore

Returns the ViewModelStore associated with this activity

Overriding this method is no longer supported and this method will be made final in a future version of ComponentActivity.

Return
ViewModelStore a ViewModelStore
Exceptions
IllegalStateException if called before the Activity is attached to the Application instance i.e., before onCreate()

onBackPressed

@MainThread open fun onBackPressed(): Unit

Called when the activity has detected the user's press of the back key. The OnBackPressedDispatcher will be given a chance to handle the back button before the default behavior of android.app.Activity#onBackPressed() is invoked.

onRequestPermissionsResult

@CallSuper open fun onRequestPermissionsResult(
    requestCode: Int,
    @NonNull permissions: Array<String!>,
    @NonNull grantResults: IntArray
): Unit

Deprecated: use registerForActivityResult(ActivityResultContract, ActivityResultCallback) passing in a RequestMultiplePermissions object for the ActivityResultContract and handling the result in the callback.

onRetainCustomNonConfigurationInstance

@Nullable open fun onRetainCustomNonConfigurationInstance(): Any?

Deprecated: Use a androidx.lifecycle.ViewModel to store non config state.

Use this instead of onRetainNonConfigurationInstance(). Retrieve later with getLastCustomNonConfigurationInstance().

onRetainNonConfigurationInstance

@Nullable fun onRetainNonConfigurationInstance(): Any?

Retain all appropriate non-config state. You can NOT override this yourself! Use a androidx.lifecycle.ViewModel if you want to retain your own non config state.

peekAvailableContext

@Nullable open fun peekAvailableContext(): Context?

registerForActivityResult

@NonNull fun <I : Any!, O : Any!> registerForActivityResult(
    @NonNull contract: ActivityResultContract<I, O>,
    @NonNull registry: ActivityResultRegistry,
    @NonNull callback: ActivityResultCallback<O>
): ActivityResultLauncher<I>

registerForActivityResult

@NonNull fun <I : Any!, O : Any!> registerForActivityResult(
    @NonNull contract: ActivityResultContract<I, O>,
    @NonNull callback: ActivityResultCallback<O>
): ActivityResultLauncher<I>

removeOnContextAvailableListener

fun removeOnContextAvailableListener(@NonNull listener: OnContextAvailableListener): Unit

setContentView

open fun setContentView(@LayoutRes layoutResID: Int): Unit

setContentView

open fun setContentView(view: View!): Unit

setContentView

open fun setContentView(
    view: View!,
    params: LayoutParams!
): Unit

startActivityForResult

open fun startActivityForResult(
    intent: Intent!,
    requestCode: Int
): Unit

Deprecated: use registerForActivityResult(ActivityResultContract, ActivityResultCallback) passing in a StartActivityForResult object for the ActivityResultContract.

startActivityForResult

open fun startActivityForResult(
    intent: Intent!,
    requestCode: Int,
    @Nullable options: Bundle?
): Unit

Deprecated: use registerForActivityResult(ActivityResultContract, ActivityResultCallback) passing in a StartActivityForResult object for the ActivityResultContract.

startIntentSenderForResult

open fun startIntentSenderForResult(
    intent: IntentSender!,
    requestCode: Int,
    @Nullable fillInIntent: Intent?,
    flagsMask: Int,
    flagsValues: Int,
    extraFlags: Int
): Unit

Deprecated: use registerForActivityResult(ActivityResultContract, ActivityResultCallback) passing in a StartIntentSenderForResult object for the ActivityResultContract.

startIntentSenderForResult

open fun startIntentSenderForResult(
    intent: IntentSender!,
    requestCode: Int,
    @Nullable fillInIntent: Intent?,
    flagsMask: Int,
    flagsValues: Int,
    extraFlags: Int,
    @Nullable options: Bundle?
): Unit

Deprecated: use registerForActivityResult(ActivityResultContract, ActivityResultCallback) passing in a StartIntentSenderForResult object for the ActivityResultContract.

Protected methods

onActivityResult

@CallSuper protected open fun onActivityResult(
    requestCode: Int,
    resultCode: Int,
    @Nullable