ComponentActivity

public class ComponentActivity
extends Activity implements LifecycleOwner, LifecycleOwner, ViewModelStoreOwner, HasDefaultViewModelProviderFactory, SavedStateRegistryOwner, OnBackPressedDispatcherOwner

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

boolean dispatchKeyEvent(KeyEvent event)
boolean dispatchKeyShortcutEvent(KeyEvent event)
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.

Object onRetainCustomNonConfigurationInstance()

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

final Object onRetainNonConfigurationInstance()

Retain all appropriate non-config state.

Protected methods

void onCreate(Bundle savedInstanceState)

If your ComponentActivity is annotated with ContentView, this will call Activity.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

dispatchKeyEvent

public boolean dispatchKeyEvent (KeyEvent event)

Parameters
event KeyEvent

Returns
boolean

dispatchKeyShortcutEvent

public boolean dispatchKeyShortcutEvent (KeyEvent event)

Parameters
event KeyEvent

Returns
boolean

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.

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

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

onSaveInstanceState

protected void onSaveInstanceState (Bundle outState)

Parameters
outState Bundle