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 super.onCreate(savedInstanceState).

Public methods

void addContentView(View view, ViewGroup.LayoutParams params)
final void addOnContextAvailableListener(OnContextAvailableListener listener)

Add a new OnContextAvailableListener for receiving a callback for when this class is associated with a Context. 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.

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.

Context peekAvailableContext()

Get the Context if it is currently available.

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.

final void removeOnContextAvailableListener(OnContextAvailableListener listener)

Remove a OnContextAvailableListener previously added via addOnContextAvailableListener(OnContextAvailableListener).

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 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