ComponentActivity
open class ComponentActivity : ComponentActivity, LifecycleOwner, ViewModelStoreOwner, HasDefaultViewModelProviderFactory, SavedStateRegistryOwner, OnBackPressedDispatcherOwner
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 |
|
---|---|
<init>() Default constructor for ComponentActivity. |
|
Alternate constructor that can be used to provide a default layout that will be inflated as part of |
Public methods |
|
---|---|
open ViewModelProvider.Factory |
Returns the default |
open Any? |
Return the value previously returned from |
open Lifecycle |
Returns the Lifecycle of the provider. |
OnBackPressedDispatcher |
Retrieve the |
SavedStateRegistry | |
open ViewModelStore |
Returns the |
open Unit |
Called when the activity has detected the user's press of the back key. |
open Any? |
Use this instead of |
Any? |
Retain all appropriate non-config state. |
Protected methods |
|
---|---|
open Unit |
If your ComponentActivity is annotated with |
open Unit |
onSaveInstanceState(@NonNull outState: Bundle) |
Extension functions |
||
---|---|---|
From androidx.activity
|
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
.
See Also
Public methods
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 fungetLastCustomNonConfigurationInstance(): 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
:
- Return an instance of
LifecycleRegistry
- 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.
See Also
onRetainCustomNonConfigurationInstance
@Nullable open funonRetainCustomNonConfigurationInstance(): 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.
Protected methods
onCreate
protected open fun onCreate(@Nullable savedInstanceState: Bundle?): Unit
If your ComponentActivity is annotated with ContentView
, this will call setContentView(int)
for you.