ComponentActivity


public class ComponentActivity extends Activity implements LifecycleOwner, ContextAware, ViewModelStoreOwner, HasDefaultViewModelProviderFactory, SavedStateRegistryOwner, OnBackPressedDispatcherOwner, ActivityResultRegistryOwner, ActivityResultCaller, OnConfigurationChangedProvider, OnTrimMemoryProvider, OnNewIntentProvider, OnMultiWindowModeChangedProvider, OnPictureInPictureModeChangedProvider, OnUserLeaveHintProvider, MenuHost, FullyDrawnReporterOwner

Known direct subclasses
FragmentActivity

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

SdkActivity

Activity to start for SDKs running locally.

Known indirect subclasses
AppCompatActivity

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

BaseCarAppActivity

Core logic for CarAppLibrary Activity interaction with a host.

CarAppActivity

The class representing a car app activity in the main display.

LauncherActivity

This class handles providing the right launcher activity when running native applications and Car App Library applications.


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

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

Adds the given MenuProvider to this MenuHost.

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

Adds the given MenuProvider to this MenuHost.

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 void

Add a new listener that will get a callback associated with ComponentCallbacks.onConfigurationChanged with the new Configuration.

final void

{@inheritDoc}

final void

Add a new listener that will get a callback associated with Activity.onMultiWindowModeChanged with the new MultiWindowModeChangedInfo.

final void

Add a new listener that will get a callback associated with Activity.onNewIntent with the new Intent.

final void

Add a new listener that will get a callback associated with Activity.onPictureInPictureModeChanged with the new PictureInPictureModeChangedInfo.

final void

Add a new listener that will get a callback associated with ComponentCallbacks2.onTrimMemory with the int representing the level of trimming.

final void

Add a new listener that will get a callback associated with Activity.onUserLeaveHint

final @NonNull ActivityResultRegistry

Get the ActivityResultRegistry associated with this activity.

@NonNull CreationExtras

{@inheritDoc}

@NonNull ViewModelProvider.Factory
@NonNull FullyDrawnReporter

Retrieve the FullyDrawnReporter that should handle the independent parts of the UI that separately report that they are fully drawn.

Object

This method is deprecated. Use a {@link androidx.lifecycle.ViewModel} to store non config state.

@NonNull Lifecycle

{@inheritDoc}

final @NonNull OnBackPressedDispatcher

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

final @NonNull SavedStateRegistry
@NonNull ViewModelStore

Returns the ViewModelStore associated with this activity

void

Sets the view tree owners before setting the content view so that the inflation process and attach listeners will see them already present.

void

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

void

This method is deprecated. This method has been deprecated in favor of using the {@link OnBackPressedDispatcher} via {@link #getOnBackPressedDispatcher()}.

void

{@inheritDoc}

boolean
onCreatePanelMenu(int featureId, @NonNull Menu menu)
boolean
onMenuItemSelected(int featureId, @NonNull MenuItem item)
void
@CallSuper
onMultiWindowModeChanged(boolean isInMultiWindowMode)

This method is deprecated. Deprecated in android.app.Activity

void
@RequiresApi(api = 26)
@CallSuper
onMultiWindowModeChanged(
    boolean isInMultiWindowMode,
    @NonNull Configuration newConfig
)

{@inheritDoc}

void
onPanelClosed(int featureId, @NonNull Menu menu)
void
@CallSuper
onPictureInPictureModeChanged(boolean isInPictureInPictureMode)

This method is deprecated. Deprecated in android.app.Activity

void
@RequiresApi(api = 26)
@CallSuper
onPictureInPictureModeChanged(
    boolean isInPictureInPictureMode,
    @NonNull Configuration newConfig
)

{@inheritDoc}

boolean
onPreparePanel(int featureId, View view, @NonNull Menu menu)
void
@CallSuper
onRequestPermissionsResult(
    int requestCode,
    @NonNull String[] permissions,
    @NonNull int[] grantResults
)

This method is deprecated. This method has been deprecated in favor of using the Activity Result API which brings increased type safety via an {@link ActivityResultContract} and the prebuilt contracts for common intents available in {@link androidx.activity.result.contract.ActivityResultContracts}, provides hooks for testing, and allow receiving results in separate, testable classes independent from your activity.

Object

This method is deprecated. Use a {@link androidx.lifecycle.ViewModel} to store non config state.

final Object

Retain all appropriate non-config state.

void

{@inheritDoc}

Context

Get the Context if it is currently available.

final @NonNull ActivityResultLauncher<@NonNull I>
<I extends Object, O extends Object> registerForActivityResult(
    @NonNull ActivityResultContract<@NonNull I, @NonNull O> contract,
    @NonNull ActivityResultCallback<@NonNull O> callback
)

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

final @NonNull ActivityResultLauncher<@NonNull I>
<I extends Object, O extends Object> registerForActivityResult(
    @NonNull ActivityResultContract<@NonNull I, @NonNull O> contract,
    @NonNull ActivityResultRegistry registry,
    @NonNull ActivityResultCallback<@NonNull O> callback
)

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

void

Removes the given MenuProvider from this MenuHost.

final void

Remove a previously added listener.

final void

Remove a OnContextAvailableListener previously added via addOnContextAvailableListener.

final void

Remove a previously added listener.

final void

Remove a previously added listener.

final void

Remove a previously added listener.

final void

Remove a previously added listener.

final void

Remove a previously added listener.

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

This method is deprecated. This method has been deprecated in favor of using the Activity Result API which brings increased type safety via an {@link ActivityResultContract} and the prebuilt contracts for common intents available in {@link androidx.activity.result.contract.ActivityResultContracts}, provides hooks for testing, and allow receiving results in separate, testable classes independent from your activity.

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

This method is deprecated. This method has been deprecated in favor of using the Activity Result API which brings increased type safety via an {@link ActivityResultContract} and the prebuilt contracts for common intents available in {@link androidx.activity.result.contract.ActivityResultContracts}, provides hooks for testing, and allow receiving results in separate, testable classes independent from your activity.

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

This method is deprecated. This method has been deprecated in favor of using the Activity Result API which brings increased type safety via an {@link ActivityResultContract} and the prebuilt contracts for common intents available in {@link androidx.activity.result.contract.ActivityResultContracts}, provides hooks for testing, and allow receiving results in separate, testable classes independent from your activity.

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

This method is deprecated. This method has been deprecated in favor of using the Activity Result API which brings increased type safety via an {@link ActivityResultContract} and the prebuilt contracts for common intents available in {@link androidx.activity.result.contract.ActivityResultContracts}, provides hooks for testing, and allow receiving results in separate, testable classes independent from your activity.

Protected methods

void
@CallSuper
onActivityResult(int requestCode, int resultCode, Intent data)

This method is deprecated. This method has been deprecated in favor of using the Activity Result API which brings increased type safety via an {@link ActivityResultContract} and the prebuilt contracts for common intents available in {@link androidx.activity.result.contract.ActivityResultContracts}, provides hooks for testing, and allow receiving results in separate, testable classes independent from your activity.

void
onCreate(Bundle savedInstanceState)

{@inheritDoc}

void

{@inheritDoc}

void
void

{@inheritDoc}

Extension functions

final @NonNull Lazy<@NonNull VM>
@MainThread
<VM extends ViewModel> ActivityViewModelLazyKt.viewModels(
    @NonNull ComponentActivity receiver,
    Function0<@NonNull CreationExtras> extrasProducer,
    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.

final void
EdgeToEdge.enableEdgeToEdge(
    @NonNull ComponentActivity receiver,
    @NonNull SystemBarStyle statusBarStyle,
    @NonNull SystemBarStyle navigationBarStyle
)

Enables the edge-to-edge display for this ComponentActivity.

Inherited methods

From android.app.Activity
void
void
void
void
@NonNull PendingIntent
createPendingResult(int p0, @NonNull Intent p1, int p2)
final void
dismissDialog(int p0)

This method is deprecated. Deprecated in Java

final void
boolean
boolean
boolean
boolean
void
dump(
    @NonNull String p0,
    @Nullable FileDescriptor p1,
    @NonNull PrintWriter p2,
    @Nullable String[] p3
)
void

This method is deprecated. Deprecated in Java

boolean
@NonNull T
<T extends View> findViewById(int p0)
void
void
void

This method is deprecated. Deprecated in Java

void
void
void
void

This method is deprecated. Deprecated in Java

@Nullable ActionBar
final @NonNull Application
@Nullable ComponentCaller
@Nullable ComponentName
@Nullable String
int
@NonNull ComponentName
@NonNull Scene
@NonNull TransitionManager
@NonNull ComponentCaller
@Nullable View
@NonNull FragmentManager

This method is deprecated. Deprecated in Java

@NonNull ComponentCaller
@NonNull Intent
@Nullable Object
@Nullable String
int
@NonNull LayoutInflater
@NonNull LoaderManager

This method is deprecated. Deprecated in Java

@NonNull String
int
final @NonNull MediaController
@NonNull MenuInflater
@NonNull OnBackInvokedDispatcher
final @NonNull Activity

This method is deprecated. Deprecated in Java

@Nullable Intent
@NonNull SharedPreferences
@Nullable Uri
int
final @NonNull SearchEvent
final @NonNull SplashScreen
@NonNull Object
int
final @NonNull CharSequence
final int
@NonNull VoiceInteractor
final int
@NonNull Window
@NonNull WindowManager
boolean
void
boolean
boolean
final boolean

This method is deprecated. Deprecated in Java

boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
final @NonNull Cursor
managedQuery(
    @NonNull Uri p0,
    @NonNull String[] p1,
    @NonNull String p2,
    @NonNull String[] p3,
    @NonNull String p4
)

This method is deprecated. Deprecated in Java

boolean
moveTaskToBack(boolean p0)
boolean
boolean

This method is deprecated. Deprecated in Java

void
void
void
void
onActivityResult(
    int p0,
    int p1,
    @Nullable Intent p2,
    @NonNull ComponentCaller p3
)
void
onApplyThemeResource(@NonNull Resources.Theme p0, int p1, boolean p2)
void

This method is deprecated. Deprecated in Java

void
void
void
boolean
void
void
void
@Nullable CharSequence
@NonNull Dialog

This method is deprecated. Deprecated in Java

@Nullable Dialog
onCreateDialog(int p0, @NonNull Bundle p1)

This method is deprecated. Deprecated in Java

void
boolean
@Nullable View
boolean

This method is deprecated. Deprecated in Java

@Nullable View
onCreateView(
    @NonNull String p0,
    @NonNull Context p1,
    @NonNull AttributeSet p2
)
@Nullable View
onCreateView(
    @Nullable View p0,
    @NonNull String p1,
    @NonNull Context p2,
    @NonNull AttributeSet p3
)
void
void
void
boolean
void
boolean
onKeyDown(int p0, @NonNull KeyEvent p1)
boolean
boolean
onKeyMultiple(int p0, int p1, @NonNull KeyEvent p2)
boolean
boolean
onKeyUp(int p0, @NonNull KeyEvent p1)
void
void
void

This method is deprecated.

boolean
onMenuOpened(int p0, @NonNull Menu p1)
boolean
boolean

This method is deprecated. Deprecated in Java

void
boolean
void
void
void
boolean
void
void
void
void
void

This method is deprecated. Deprecated in Java

void

This method is deprecated. Deprecated in Java

void
boolean
void
void
void
onProvideKeyboardShortcuts(
    @NonNull List<@NonNull KeyboardShortcutGroup> p0,
    Menu p1,
    int p2
)
@NonNull Uri
void
onRequestPermissionsResult(
    int p0,
    @NonNull String[] p1,
    @NonNull int[] p2,
    int p3
)
void
void
void
void
void
boolean
boolean
void
void

This method is deprecated. Deprecated in Java

void
void
void
boolean
boolean
void
void

This method is deprecated. Deprecated in Java

void
void
@Nullable ActionMode
@Nullable A