ComponentActivity
public
class
ComponentActivity
extends Activity
implements
LifecycleOwner,
ViewModelStoreOwner,
SavedStateRegistryOwner
| java.lang.Object | |||||
| ↳ | android.content.Context | ||||
| ↳ | android.content.ContextWrapper | ||||
| ↳ | android.view.ContextThemeWrapper | ||||
| ↳ | android.app.Activity | ||||
| ↳ | androidx.activity.ComponentActivity | ||||
|
Known direct subclasses
|
|
Known indirect subclasses
|
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
Inherited constants | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
From class
android.app.Activity
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
From class
android.content.Context
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
From interface
android.content.ComponentCallbacks2
|
Inherited fields | ||
|---|---|---|
|
From class
android.app.Activity
|
Public constructors | |
|---|---|
ComponentActivity()
Default constructor for ComponentActivity. |
|
ComponentActivity(int contentLayoutId)
Alternate constructor that can be used to provide a default layout
that will be inflated as part of |
|
Public methods | |
|---|---|
void
|
addOnBackPressedCallback(LifecycleOwner owner, OnBackPressedCallback onBackPressedCallback)
This method is deprecated.
Use |
void
|
addOnBackPressedCallback(OnBackPressedCallback onBackPressedCallback)
This method is deprecated.
Use |
boolean
|
dispatchKeyEvent(KeyEvent event)
|
boolean
|
dispatchKeyShortcutEvent(KeyEvent event)
|
Object
|
getLastCustomNonConfigurationInstance()
This method is deprecated.
Use a |
Lifecycle
|
getLifecycle()
Returns the Lifecycle of the provider.
Overriding this method is no longer supported and this method will be made
|
final
OnBackPressedDispatcher
|
getOnBackPressedDispatcher()
Retrieve the |
final
SavedStateRegistry
|
getSavedStateRegistry()
Returns owned |
ViewModelStore
|
getViewModelStore()
Returns the
Overriding this method is no longer supported and this method will be made
|
void
|
onBackPressed()
Called when the activity has detected the user's press of the back key. |
Object
|
onRetainCustomNonConfigurationInstance()
This method is deprecated.
Use a |
final
Object
|
onRetainNonConfigurationInstance()
Retain all appropriate non-config state. |
void
|
removeOnBackPressedCallback(OnBackPressedCallback onBackPressedCallback)
This method is deprecated.
Use |
Protected methods | |
|---|---|
void
|
onCreate(Bundle savedInstanceState)
If your ComponentActivity is annotated with |
void
|
onSaveInstanceState(Bundle outState)
|
Inherited methods | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
From class
android.app.Activity
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
From class
android.view.ContextThemeWrapper
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
From class
android.content.ContextWrapper
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
From class
android.content.Context
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
From class
java.lang.Object
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
From interface
android.view.LayoutInflater.Factory2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
From interface
android.view.Window.Callback
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
From interface
android.view.KeyEvent.Callback
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
From interface
android.view.View.OnCreateContextMenuListener
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
From interface
android.content.ComponentCallbacks2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
From interface
androidx.lifecycle.LifecycleOwner
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
From interface
androidx.lifecycle.ViewModelStoreOwner
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
From interface
androidx.savedstate.SavedStateRegistryOwner
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
From interface
android.view.LayoutInflater.Factory
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
From interface
android.content.ComponentCallbacks
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Public constructors
ComponentActivity
public ComponentActivity ()
Default constructor for ComponentActivity. All Activities must have a default constructor
for API 27 and lower devices or when using the default
AppComponentFactory.
ComponentActivity
public ComponentActivity (int contentLayoutId)
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
AppComponentFactory.
| Parameters | |
|---|---|
contentLayoutId |
int |
See also:
Public methods
addOnBackPressedCallback
public void addOnBackPressedCallback (LifecycleOwner owner, OnBackPressedCallback onBackPressedCallback)
This method is deprecated.
Use and
{@link OnBackPressedDispatcher#addCallback(LifecycleOwner, OnBackPressedCallback)}.
Add a new OnBackPressedCallback. Callbacks are invoked in order of recency, so
this newly added OnBackPressedCallback will be the first callback to receive a
callback if onBackPressed() is called. Only if this callback returns
false from its OnBackPressedCallback.handleOnBackPressed() will any
previously added callback be called.
The OnBackPressedCallback.handleOnBackPressed() callback will only be called if the
given LifecycleOwner is at least Lifecycle.State.STARTED. When the
LifecycleOwner is destroyed, it will automatically
be removed from the list of callbacks. The only time you would need to manually call
removeOnBackPressedCallback(OnBackPressedCallback) is if you'd like to remove the
callback prior to destruction of the associated lifecycle.
| Parameters | |
|---|---|
owner |
LifecycleOwner: The LifecycleOwner which controls when the callback should be invoked |
onBackPressedCallback |
OnBackPressedCallback: The callback to add |
addOnBackPressedCallback
public void addOnBackPressedCallback (OnBackPressedCallback onBackPressedCallback)
This method is deprecated.
Use and
{@link OnBackPressedDispatcher#addCallback(LifecycleOwner, OnBackPressedCallback)},
explicitly passing in this Activity object as the LifecycleOwner.
Add a new OnBackPressedCallback. Callbacks are invoked in order of recency, so
this newly added OnBackPressedCallback will be the first callback to receive a
callback if onBackPressed() is called. Only if this callback returns
false from its OnBackPressedCallback.handleOnBackPressed() will any
previously added callback be called.
This is the equivalent of passing this ComponentActivity to
addOnBackPressedCallback(LifecycleOwner, OnBackPressedCallback) and ensures that
the OnBackPressedCallback.handleOnBackPressed() callback will only be called
if this ComponentActivity is at least Lifecycle.State.STARTED. You can
remove the callback prior to the destruction of your activity by calling
removeOnBackPressedCallback(OnBackPressedCallback).
| Parameters | |
|---|---|
onBackPressedCallback |
OnBackPressedCallback: The callback to add |
dispatchKeyEvent
public boolean dispatchKeyEvent (KeyEvent event)
| Parameters | |
|---|---|
event |
KeyEvent |
| Returns | |
|---|---|
boolean |
|
dispatchKeyShortcutEvent
public boolean dispatchKeyShortcutEvent (KeyEvent event)
| Parameters | |
|---|---|
event |
KeyEvent |
| Returns | |
|---|---|
boolean |
|
getLastCustomNonConfigurationInstance
public Object getLastCustomNonConfigurationInstance ()
This method is deprecated.
Use a ViewModel to store non config state.
Return the value previously returned from
onRetainCustomNonConfigurationInstance().
| Returns | |
|---|---|
Object |
|
getLifecycle
public Lifecycle getLifecycle ()
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.
| Returns | |
|---|---|
Lifecycle |
The lifecycle of the provider. |
getOnBackPressedDispatcher
public final OnBackPressedDispatcher getOnBackPressedDispatcher ()
Retrieve the OnBackPressedDispatcher that will be triggered when
onBackPressed() is called.
| Returns | |
|---|---|
OnBackPressedDispatcher |
The OnBackPressedDispatcher associated with this ComponentActivity.
|
getSavedStateRegistry
public final SavedStateRegistry getSavedStateRegistry ()
Returns owned SavedStateRegistry
| Returns | |
|---|---|
SavedStateRegistry |
a SavedStateRegistry
|
getViewModelStore
public ViewModelStore getViewModelStore ()
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.
| Returns | |
|---|---|
ViewModelStore |
a ViewModelStore |
| Throws | |
|---|---|
IllegalStateException |
if called before the Activity is attached to the Application instance i.e., before onCreate() |
onBackPressed
public void onBackPressed ()
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
Activity.onBackPressed() is invoked.
See also:
onRetainCustomNonConfigurationInstance
public Object onRetainCustomNonConfigurationInstance ()
This method is deprecated.
Use a ViewModel to store non config state.
Use this instead of onRetainNonConfigurationInstance().
Retrieve later with getLastCustomNonConfigurationInstance().
| Returns | |
|---|---|
Object |
|
onRetainNonConfigurationInstance
public final Object onRetainNonConfigurationInstance ()
Retain all appropriate non-config state. You can NOT
override this yourself! Use a ViewModel if you want to
retain your own non config state.
| Returns | |
|---|---|
Object |
|
removeOnBackPressedCallback
public void removeOnBackPressedCallback (OnBackPressedCallback onBackPressedCallback)
This method is deprecated.
Use Cancellable.cancel() on the
Cancellable returned by and
{@link OnBackPressedDispatcher#addCallback }.
Remove a previously
added
OnBackPressedCallback instance. The callback won't be called for any future
onBackPressed() calls, but may still receive a callback if this method is called
during the dispatch of an ongoing onBackPressed() call.
This call is usually not necessary as callbacks will be automatically removed when their
associated LifecycleOwner is destroyed.
| Parameters | |
|---|---|
onBackPressedCallback |
OnBackPressedCallback: The callback to remove |
Protected methods
onCreate
protected void onCreate (Bundle savedInstanceState)
If your ComponentActivity is annotated with ContentView, this will
call Activity.setContentView(int) for you.
| Parameters | |
|---|---|
savedInstanceState |
Bundle |
Interfaces
Classes