ComponentActivity

public class ComponentActivity implements ContextAware, LifecycleOwner, ViewModelStoreOwner, HasDefaultViewModelProviderFactory, LifecycleOwner, SavedStateRegistryOwner, LifecycleOwner, OnBackPressedDispatcherOwner, ActivityResultRegistryOwner, ActivityResultCaller, MenuHost

Known direct subclasses
FragmentActivity

Base class for activities that want to use the support-based Fragments.

Known indirect subclasses
AppCompatActivity

Base class for activities that wish to use some of the newer platform features on older Android devices.

CarAppActivity

The class representing a car app activity.


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

Default constructor for ComponentActivity.

ComponentActivity(@LayoutRes @NonNull 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

@Override @NonNull void
@Override @NonNull void

Adds the given MenuProvider to this MenuHost.

@Override @NonNull void
addMenuProvider(
    @NonNull MenuProvider provider,
    @NonNull LifecycleOwner owner
)

Adds the given MenuProvider to this MenuHost.

@Override @NonNull void
addMenuProvider(
    @NonNull MenuProvider provider,
    @NonNull LifecycleOwner owner,
    @NonNull Lifecycle.State state
)

Adds the given MenuProvider to this MenuHost once the given LifecycleOwner reaches the given Lifecycle.State.

final @Override @NonNull void

Add a new for receiving a callback for when this class is associated with a .

final @NonNull @Override ActivityResultRegistry

Get the ActivityResultRegistry associated with this activity.

@NonNull @Override ViewModelProvider.Factory

Returns the default that should be used when no custom Factory} is provided to the constructors.

@Nullable Object

This method is deprecated.

Use a androidx.lifecycle.ViewModel to store non config state.

@NonNull @Override Lifecycle

Overriding this method is no longer supported and this method will be made final in a future version of ComponentActivity.

final @NonNull @Override OnBackPressedDispatcher

Retrieve the OnBackPressedDispatcher that will be triggered when onBackPressed is called.

final @NonNull @Override SavedStateRegistry

Returns owned SavedStateRegistry

@NonNull @Override ViewModelStore

Returns the ViewModelStore associated with this activity

@Override @NonNull void

Invalidates the android.view.Menu to ensure that what is displayed matches the current internal state of the menu.

@Override @MainThread @NonNull void

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

@Override @NonNull boolean
@Override @NonNull boolean
@CallSuper @Override @NonNull void
onRequestPermissionsResult(
    @NonNull int requestCode,
    @NonNull Array<@NonNull String> permissions,
    @NonNull Array<@NonNull int> grantResults
)

This method is deprecated.

use registerForActivityResult passing in a RequestMultiplePermissions object for the ActivityResultContract and handling the result in the callback.

@Nullable Object

This method is deprecated.

Use a androidx.lifecycle.ViewModel to store non config state.

final @Override @Nullable Object

Retain all appropriate non-config state.

@Nullable @Override Context

Get the Context if it is currently available.

final @NonNull @Override ActivityResultLauncher<@NonNull I>

Register a request to start an activity for result, designated by the given contract.

final @NonNull @Override ActivityResultLauncher<@NonNull I>

Register a request to start an activity for result, designated by the given contract.

@Override @NonNull void

Removes the given MenuProvider from this MenuHost.

final @Override @NonNull void

Remove a OnContextAvailableListener previously added via addOnContextAvailableListener.

@Override @NonNull void
@Override @NonNull void
setContentView(@LayoutRes @NonNull int layoutResID)
@Override @NonNull void
@Override @NonNull void
@Override @NonNull void
startActivityForResult(@NonNull Intent intent, @NonNull int requestCode)

This method is deprecated.

use registerForActivityResult passing in a StartActivityForResult object for the ActivityResultContract.

@Override @NonNull void
startActivityForResult(
    @NonNull Intent intent,
    @NonNull int requestCode,
    @Nullable Bundle options
)

This method is deprecated.

use registerForActivityResult passing in a StartActivityForResult object for the ActivityResultContract.

@Override @NonNull void
startIntentSenderForResult(
    @NonNull IntentSender intent,
    @NonNull int requestCode,
    @Nullable Intent fillInIntent,
    @NonNull int flagsMask,
    @NonNull int flagsValues,
    @NonNull int extraFlags
)

This method is deprecated.

use registerForActivityResult passing in a StartIntentSenderForResult object for the ActivityResultContract.

@Override @NonNull void
startIntentSenderForResult(
    @NonNull IntentSender intent,
    @NonNull int requestCode,
    @Nullable Intent fillInIntent,
    @NonNull int flagsMask,
    @NonNull int flagsValues,
    @NonNull int extraFlags,
    @Nullable Bundle options
)

This method is deprecated.

use registerForActivityResult passing in a StartIntentSenderForResult object for the ActivityResultContract.

Extension functions

final @MainThread @NonNull Lazy<@NonNull VM>
<VM extends ViewModel> ActivityViewModelLazyKt.viewModels(
    @NonNull ComponentActivity receiver,
    @Nullable Function0<@NonNull ViewModelProvider.Factory> factoryProducer
)

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.

Inherited methods

From class Activity
@NonNull void
@NonNull void
@NonNull PendingIntent
createPendingResult(
    @NonNull int requestCode,
    @NonNull Intent data,
    @NonNull int flags
)
final @NonNull void

This method is deprecated.

final @NonNull void
@NonNull boolean
@NonNull boolean
@NonNull boolean
@NonNull boolean
@NonNull boolean
@NonNull boolean
@NonNull void
dump(
    @NonNull String prefix,
    @NonNull FileDescriptor fd,
    @NonNull PrintWriter writer,
    @NonNull Array<@NonNull String> args
)
@NonNull void

This method is deprecated.

@NonNull boolean