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