Únete a ⁠ #Android11: The Beta Launch Show el 3 de junio.

ComponentActivity

public class ComponentActivity
extends Activity implements LifecycleOwner, 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 super.onCreate(savedInstanceState).

Public methods

void addContentView(View view, ViewGroup.LayoutParams params)
boolean dispatchKeyEvent(KeyEvent event)
boolean dispatchKeyShortcutEvent(KeyEvent event)
final ActivityResultRegistry getActivityResultRegistry()

Get the ActivityResultRegistry associated with this activity.

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.

Object getLastCustomNonConfigurationInstance()

This method is deprecated. Use a ViewModel to store non config state.

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.

final OnBackPressedDispatcher getOnBackPressedDispatcher()

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

final SavedStateRegistry getSavedStateRegistry()

Returns owned SavedStateRegistry

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.

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 registerForActivityResult(ActivityResultContract, ActivityResultCallback) passing in a ActivityResultContracts.RequestMultiplePermissions object for the ActivityResultContract and handling the result in the callback.

Object onRetainCustomNonConfigurationInstance()

This method is deprecated. Use a ViewModel to store non config state.

final Object onRetainNonConfigurationInstance()

Retain all appropriate non-config state.

final <I, O> ActivityResultLauncher<I> registerForActivityResult(ActivityResultContract<I, O> contract, ActivityResultCallback<O> callback)

Register a request to start an activity for result, designated by the given contract.

final <I, O> 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.

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 registerForActivityResult(ActivityResultContract, ActivityResultCallback) passing in a ActivityResultContracts.StartActivityForResult object for the ActivityResultContract.

void startActivityForResult(Intent intent, int requestCode)

This method is deprecated. use registerForActivityResult(ActivityResultContract, ActivityResultCallback) passing in a ActivityResultContracts.StartActivityForResult object for the ActivityResultContract.

void startIntentSenderForResult(IntentSender intent, int requestCode, Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)

This method is deprecated. use registerForActivityResult(ActivityResultContract, ActivityResultCallback) passing in a ActivityResultContracts.StartIntentSenderForResult object for the ActivityResultContract.

void startIntentSenderForResult(IntentSender intent, int requestCode, Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options)

This method is deprecated. use registerForActivityResult(ActivityResultContract, ActivityResultCallback) passing in a ActivityResultContracts.StartIntentSenderForResult object for the ActivityResultContract.

Protected methods

void onActivityResult(int requestCode, int resultCode, Intent data)

This method is deprecated. use registerForActivityResult(ActivityResultContract, ActivityResultCallback) with the appropriate ActivityResultContract and handling the result in the callback.

void onCreate(Bundle savedInstanceState)

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

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

Public methods

addContentView

public void addContentView (View view, 
                ViewGroup.LayoutParams params)

Parameters
view View

params ViewGroup.LayoutParams

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:

  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.

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.

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

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 Intents

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 Intents

registry ActivityResultRegistry: the registry where to hold the record.

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.

setContentView

public void setContentView (View view)

Parameters
view View

setContentView

public void setContentView (int layoutResID)

Parameters
layoutResID int

setContentView

public void setContentView (View view, 
                ViewGroup.LayoutParams params)

Parameters
view View

params ViewGroup.LayoutParams

startActivityForResult

public void startActivityForResult (Intent intent, 
                int requestCode, 
                Bundle options)

This method is deprecated.
use registerForActivityResult(ActivityResultContract, ActivityResultCallback) passing in a ActivityResultContracts.StartActivityForResult object for the ActivityResultContract.

Parameters
intent Intent

requestCode int

options Bundle

startActivityForResult

public void startActivityForResult (Intent intent, 
                int requestCode)

This method is deprecated.
use registerForActivityResult(ActivityResultContract, ActivityResultCallback) passing in a ActivityResultContracts.StartActivityForResult object for the ActivityResultContract.

Parameters
intent Intent

requestCode int

startIntentSenderForResult

public void startIntentSenderForResult (IntentSender intent, 
                int requestCode, 
                Intent fillInIntent, 
                int flagsMask, 
                int flagsValues, 
                int extraFlags)

This method is deprecated.
use registerForActivityResult(ActivityResultContract, ActivityResultCallback) passing in a ActivityResultContracts.StartIntentSenderForResult object for the ActivityResultContract.

Parameters
intent IntentSender

requestCode int

fillInIntent Intent

flagsMask int

flagsValues int

extraFlags int

Throws
IntentSender.SendIntentException

startIntentSenderForResult

public void startIntentSenderForResult (IntentSender intent, 
                int requestCode, 
                Intent fillInIntent, 
                int flagsMask, 
                int flagsValues, 
                int extraFlags, 
                Bundle options)

This method is deprecated.
use registerForActivityResult(ActivityResultContract, ActivityResultCallback) passing in a ActivityResultContracts.StartIntentSenderForResult object for the ActivityResultContract.

Parameters
intent IntentSender

requestCode int

fillInIntent Intent

flagsMask int

flagsValues int

extraFlags int

options Bundle

Throws
IntentSender.SendIntentException

Protected methods

onActivityResult

protected void onActivityResult (int requestCode, 
                int resultCode, 
                Intent data)

This method is deprecated.
use registerForActivityResult(ActivityResultContract, ActivityResultCallback) with the appropriate ActivityResultContract and handling the result in the callback.

Parameters
requestCode int

resultCode int

data Intent

onCreate

protected void onCreate (Bundle savedInstanceState)

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

Parameters
savedInstanceState Bundle

onSaveInstanceState

protected void onSaveInstanceState (Bundle outState)

Parameters
outState Bundle