ComponentActivity

public class ComponentActivity
extends Activity implements LifecycleOwner, LifecycleOwner, ViewModelStoreOwner

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

Public methods

void addOnBackPressedCallback(LifecycleOwner owner, OnBackPressedCallback onBackPressedCallback)

Add a new OnBackPressedCallback.

void addOnBackPressedCallback(OnBackPressedCallback onBackPressedCallback)

Add a new OnBackPressedCallback.

boolean dispatchKeyEvent(KeyEvent event)
boolean dispatchKeyShortcutEvent(KeyEvent event)
Object getLastCustomNonConfigurationInstance()

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

Lifecycle getLifecycle()

Returns the Lifecycle of the provider.

ViewModelStore getViewModelStore()

Returns the ViewModelStore associated with this activity

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.

void removeOnBackPressedCallback(OnBackPressedCallback onBackPressedCallback)

Remove a previously added OnBackPressedCallback instance.

Protected methods

void onCreate(Bundle savedInstanceState)
void onSaveInstanceState(Bundle outState)

Inherited methods

Public constructors

ComponentActivity

public ComponentActivity ()

Public methods

addOnBackPressedCallback

public void addOnBackPressedCallback (LifecycleOwner owner, 
                OnBackPressedCallback onBackPressedCallback)

Add a new OnBackPressedCallback. Callbacks are invoked in order of recency, so this newly added OnBackPressedCallback will be the first callback to receive a callback if onBackPressed() is called. Only if this callback returns false from its OnBackPressedCallback.handleOnBackPressed() will any previously added callback be called.

The OnBackPressedCallback.handleOnBackPressed() callback will only be called if the given LifecycleOwner is at least Lifecycle.State.STARTED. When the LifecycleOwner is destroyed, it will automatically be removed from the list of callbacks. The only time you would need to manually call removeOnBackPressedCallback(OnBackPressedCallback) is if you'd like to remove the callback prior to destruction of the associated lifecycle.

Parameters
owner LifecycleOwner: The LifecycleOwner which controls when the callback should be invoked

onBackPressedCallback OnBackPressedCallback: The callback to add

addOnBackPressedCallback

public void addOnBackPressedCallback (OnBackPressedCallback onBackPressedCallback)

Add a new OnBackPressedCallback. Callbacks are invoked in order of recency, so this newly added OnBackPressedCallback will be the first callback to receive a callback if onBackPressed() is called. Only if this callback returns false from its OnBackPressedCallback.handleOnBackPressed() will any previously added callback be called.

This is the equivalent of passing this ComponentActivity to addOnBackPressedCallback(LifecycleOwner, OnBackPressedCallback) and ensures that the OnBackPressedCallback.handleOnBackPressed() callback will only be called if this ComponentActivity is at least Lifecycle.State.STARTED. You can remove the callback prior to the destruction of your activity by calling removeOnBackPressedCallback(OnBackPressedCallback).

Parameters
onBackPressedCallback OnBackPressedCallback: The callback to add

dispatchKeyEvent

public boolean dispatchKeyEvent (KeyEvent event)

Parameters
event KeyEvent

Returns
boolean

dispatchKeyShortcutEvent

public boolean dispatchKeyShortcutEvent (KeyEvent event)

Parameters
event KeyEvent

Returns
boolean

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.

Returns
Lifecycle The lifecycle of the provider.

getViewModelStore

public ViewModelStore getViewModelStore ()

Returns the ViewModelStore associated with this activity

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. Any OnBackPressedCallback added via addOnBackPressedCallback(LifecycleOwner, OnBackPressedCallback) 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

removeOnBackPressedCallback

public void removeOnBackPressedCallback (OnBackPressedCallback onBackPressedCallback)

Remove a previously added OnBackPressedCallback instance. The callback won't be called for any future onBackPressed() calls, but may still receive a callback if this method is called during the dispatch of an ongoing onBackPressed() call.

This call is usually not necessary as callbacks will be automatically removed when their associated LifecycleOwner is destroyed.

Parameters
onBackPressedCallback OnBackPressedCallback: The callback to remove

Protected methods

onCreate

protected void onCreate (Bundle savedInstanceState)

Parameters
savedInstanceState Bundle

onSaveInstanceState

protected void onSaveInstanceState (Bundle outState)

Parameters
outState Bundle