ComponentActivity
public
class
ComponentActivity
extends Activity
implements
LifecycleOwner,
ContextAware,
LifecycleOwner,
ViewModelStoreOwner,
HasDefaultViewModelProviderFactory,
SavedStateRegistryOwner,
OnBackPressedDispatcherOwner,
ActivityResultRegistryOwner,
ActivityResultCaller
java.lang.Object | |||||
↳ | android.content.Context | ||||
↳ | android.content.ContextWrapper | ||||
↳ | android.view.ContextThemeWrapper | ||||
↳ | android.app.Activity | ||||
↳ | 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
Inherited constants |
---|
Inherited fields |
---|
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
|
addContentView(View view, ViewGroup.LayoutParams params)
|
final
void
|
addOnContextAvailableListener(OnContextAvailableListener listener)
Add a new |
boolean
|
dispatchKeyEvent(KeyEvent event)
|
boolean
|
dispatchKeyShortcutEvent(KeyEvent event)
|
final
ActivityResultRegistry
|
getActivityResultRegistry()
Get the |
ViewModelProvider.Factory
|
getDefaultViewModelProviderFactory()
Returns the default The extras of |
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. |
void
|
onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
This method is deprecated.
use
|
Object
|
onRetainCustomNonConfigurationInstance()
This method is deprecated.
Use a |
final
Object
|
onRetainNonConfigurationInstance()
Retain all appropriate non-config state. |
Context
|
peekAvailableContext()
Get the |
final
<I, O>
ActivityResultLauncher<I>
|
registerForActivityResult(ActivityResultContract<I, O> contract, ActivityResultCallback<O> callback)
Register a request to |
final
<I, O>
ActivityResultLauncher<I>
|
registerForActivityResult(ActivityResultContract<I, O> contract, ActivityResultRegistry registry, ActivityResultCallback<O> callback)
Register a request to |
final
void
|
removeOnContextAvailableListener(OnContextAvailableListener listener)
Remove a |
void
|
reportFullyDrawn()
|
void
|
setContentView(View view)
|
void
|
setContentView(int layoutResID)
|
void
|
setContentView(View view, ViewGroup.LayoutParams params)
|
void
|
startActivityForResult(Intent intent, int requestCode, Bundle options)
This method is deprecated.
use
|
void
|
startActivityForResult(Intent intent, int requestCode)
This method is deprecated.
use
|
void
|
startIntentSenderForResult(IntentSender intent, int requestCode, Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
This method is deprecated.
use
|
void
|
startIntentSenderForResult(IntentSender intent, int requestCode, Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options)
This method is deprecated.
use
|
Protected methods | |
---|---|
void
|
onActivityResult(int requestCode, int resultCode, Intent data)
This method is deprecated.
use
|
void
|
onCreate(Bundle savedInstanceState)
If your ComponentActivity is annotated with |
void
|
onSaveInstanceState(Bundle outState)
|
Inherited methods | |
---|---|
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
addContentView
public void addContentView (View view, ViewGroup.LayoutParams params)
Parameters | |
---|---|
view |
View |
params |
ViewGroup.LayoutParams |
addOnContextAvailableListener
public final void addOnContextAvailableListener (OnContextAvailableListener listener)
Add a new OnContextAvailableListener
for receiving a callback for when
this class is associated with a 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. |
dispatchKeyEvent
public boolean dispatchKeyEvent (KeyEvent event)
Parameters | |
---|---|
event |
KeyEvent |
Returns | |
---|---|
boolean |
dispatchKeyShortcutEvent
public boolean dispatchKeyShortcutEvent (KeyEvent event)
Parameters | |
---|---|
event |
KeyEvent |
Returns | |
---|---|
boolean |
getActivityResultRegistry
public final ActivityResultRegistry getActivityResultRegistry ()
Get the ActivityResultRegistry
associated with this activity.
Returns | |
---|---|
ActivityResultRegistry |
the ActivityResultRegistry
|
getDefaultViewModelProviderFactory
public ViewModelProvider.Factory getDefaultViewModelProviderFactory ()
Returns the default ViewModelProvider.Factory
that should be
used when no custom Factory
is provided to the
ViewModelProvider
constructors.
The extras of Activity.getIntent()
when this is first called will be used as
the defaults to any SavedStateHandle
passed to a view model
created using this factory.
Returns | |
---|---|
ViewModelProvider.Factory |
a ViewModelProvider.Factory
|
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:
onRequestPermissionsResult
public void onRequestPermissionsResult (int requestCode, String[] permissions, int[] grantResults)
This method is deprecated.
use
registerForActivityResult(ActivityResultContract, ActivityResultCallback)
passing
in a ActivityResultContracts.RequestMultiplePermissions
object for the ActivityResultContract
and
handling the result in the callback
.
Parameters | |
---|---|
requestCode |
int |
permissions |
String |
grantResults |
int |
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 |
peekAvailableContext
public Context peekAvailableContext ()
Get the Context
if it is currently available. If this returns
null
, you can use
addOnContextAvailableListener(OnContextAvailableListener)
to receive
a callback for when it available.
Returns | |
---|---|
Context |
the Context if it is currently available. |
registerForActivityResult
public final ActivityResultLauncher<I> registerForActivityResult (ActivityResultContract<I, O> contract, ActivityResultCallback<O> callback)
Register a request to start an activity for result
,
designated by the given contract
.
This creates a record in the registry
associated wit this
caller, managing request code, as well as conversions to/from Intent
under the hood.
This *must* be called unconditionally, as part of initialization path, typically as a field
initializer of an Activity or Fragment.
Parameters | |
---|---|
contract |
ActivityResultContract : the contract, specifying conversions to/from Intent s |
callback |
ActivityResultCallback : the callback to be called on the main thread when activity result
is available |
Returns | |
---|---|
ActivityResultLauncher<I> |
the launcher that can be used to start the activity or dispose of the prepared call. |
registerForActivityResult
public final ActivityResultLauncher<I> registerForActivityResult (ActivityResultContract<I, O> contract, ActivityResultRegistry registry, ActivityResultCallback<O> callback)
Register a request to start an activity for result
,
designated by the given contract
.
This creates a record in the given registry
, managing request
code, as well as conversions to/from Intent
under the hood.
This *must* be called unconditionally, as part of initialization path, typically as a field
initializer of an Activity or Fragment.
Parameters | |
---|---|
contract |
ActivityResultContract : the contract, specifying conversions to/from Intent s |
|