ComponentActivity

open class ComponentActivity : ComponentActivity, LifecycleOwner, ViewModelStoreOwner, BundleSavedStateRegistryOwner
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

Public constructors

Public methods
open Any?

Return the value previously returned from onRetainCustomNonConfigurationInstance().

open Lifecycle

Returns the Lifecycle of the provider.

open Unit
addOnBackPressedCallback(@NonNull onBackPressedCallback: OnBackPressedCallback)

Add a new OnBackPressedCallback.

open Unit
addOnBackPressedCallback(@NonNull owner: LifecycleOwner, @NonNull onBackPressedCallback: OnBackPressedCallback)

Add a new OnBackPressedCallback.

SavedStateRegistry<Bundle!>

open ViewModelStore

Returns the ViewModelStore associated with this activity

open Unit

Called when the activity has detected the user's press of the back key.

Any?

Retain all appropriate non-config state.

open Any?

Use this instead of onRetainNonConfigurationInstance().

open Unit
removeOnBackPressedCallback(@NonNull onBackPressedCallback: OnBackPressedCallback)

Remove a previously added OnBackPressedCallback instance.

Protected methods
open Unit
onCreate(@Nullable savedInstanceState: Bundle?)

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

open Unit
onSaveInstanceState(@NonNull outState: Bundle)

Extension functions
From androidx.activity
Lazy<VM>
ComponentActivity.viewModels(noinline factoryProducer: () -> ViewModelProvider.Factory = null)

Returns a Lazy delegate to access the ComponentActivity's ViewModel, if factoryProducer is specified then ViewModelProvider.Factory returned by it will be used to create ViewModel first time.

Public constructors

<init>

ComponentActivity()

Public methods

getLastCustomNonConfigurationInstance

@Nullable open fun getLastCustomNonConfigurationInstance(): Any?

Return the value previously returned from onRetainCustomNonConfigurationInstance().

getLifecycle

@NonNull open fun getLifecycle(): Lifecycle

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.
Return
Lifecycle: The lifecycle of the provider.

addOnBackPressedCallback

open fun addOnBackPressedCallback(@NonNull onBackPressedCallback: OnBackPressedCallback): Unit

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

addOnBackPressedCallback

open fun addOnBackPressedCallback(@NonNull owner: LifecycleOwner, @NonNull onBackPressedCallback: OnBackPressedCallback): Unit

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 LifecycleOwner: The callback to add

getBundleSavedStateRegistry

@NonNull fun getBundleSavedStateRegistry(): SavedStateRegistry<Bundle!>

getViewModelStore

@NonNull open fun getViewModelStore(): ViewModelStore

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.

Return
ViewModelStore: a ViewModelStore
Exceptions
IllegalStateException if called before the Activity is attached to the Application instance i.e., before onCreate()

onBackPressed

open fun onBackPressed(): Unit

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 android.app.Activity#onBackPressed() is invoked.

onRetainNonConfigurationInstance

@Nullable fun onRetainNonConfigurationInstance(): Any?

Retain all appropriate non-config state. You can NOT override this yourself! Use a androidx.lifecycle.ViewModel if you want to retain your own non config state.

onRetainCustomNonConfigurationInstance

@Nullable open fun onRetainCustomNonConfigurationInstance(): Any?

Use this instead of onRetainNonConfigurationInstance(). Retrieve later with getLastCustomNonConfigurationInstance().

removeOnBackPressedCallback

open fun removeOnBackPressedCallback(@NonNull onBackPressedCallback: OnBackPressedCallback): Unit

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 open fun onCreate(@Nullable savedInstanceState: Bundle?): Unit

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

onSaveInstanceState

@CallSuper protected open fun onSaveInstanceState(@NonNull outState: Bundle): Unit