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

boolean
boolean
final @NonNull ActivityResultRegistry

Get the ActivityResultRegistry associated with this activity.

@NonNull CreationExtras

{@inheritDoc}

@NonNull ViewModelProvider.Factory

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

@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

Returns the Lifecycle of the provider.

final @NonNull OnBackPressedDispatcher

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

final @NonNull SavedStateRegistry

The SavedStateRegistry owned by this SavedStateRegistryOwner

@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}

final boolean
shouldDumpInternalState(String[] args)

Checks if the internal state should be dump, as some special args are handled by Activity itself.

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 ComponentName
@Nullable String
int
@NonNull ComponentName
@NonNull Scene
@NonNull TransitionManager
@Nullable View
@NonNull FragmentManager

This method is deprecated. Deprecated in Java

@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
@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
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
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
boolean
onMenuOpened(int p0, @NonNull Menu p1)
boolean
boolean

This method is deprecated. Deprecated in Java

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
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 ActionMode
void
void
void
overrideActivityTransition(int p0, int p1, int p2)
void
overrideActivityTransition(int p0, int p1, int p2, int p3)
void
overridePendingTransition(int p0, int p1)

This method is deprecated. Deprecated in Java

void
overridePendingTransition(int p0, int p1, int p2)

This method is deprecated. Deprecated in Java

void
void
void
void
void
void
boolean
final void
removeDialog(int p0)

This method is deprecated. Deprecated in Java

@NonNull DragAndDropPermissions
void
final void
requestPermissions(@NonNull String[] p0, int p1)
final void
boolean

This method is deprecated. Deprecated in Java

final boolean
final @NonNull T
<T extends View> requireViewById(int p0)
final void
void
void
final void
void
void
final void
final void
setFeatureDrawableAlpha(int p0, int p1)
final void
setFeatureDrawableResource(int p0, int p1)
final void
void
void
setImmersive(boolean p0)
void
void
void
final void
void
final void
setProgress(int p0)

This method is deprecated. Deprecated in Java

final void

This method is deprecated. Deprecated in Java

final void

This method is deprecated. Deprecated in Java

final void

This method is deprecated. Deprecated in Java

void
void
final void
setResult(int p0)
final void
setResult(int p0, @NonNull Intent p1)
final void

This method is deprecated. Deprecated in Java

void
void
setShowWhenLocked(boolean p0)
void
void
setTheme(int p0)
void
void
setTitle(int p0)
void
setTitleColor(int p0)

This method is deprecated. Deprecated in Java

boolean
setTranslucent(boolean p0)
void
setTurnScreenOn(boolean p0)
void
setVisible(boolean p0)
final void
void
boolean
boolean
boolean
boolean
final void
showDialog(int p0)

This method is deprecated. Deprecated in Java

final boolean
showDialog(int p0, @NonNull Bundle p1)

This method is deprecated. Deprecated in Java

void
@Nullable ActionMode
@Nullable ActionMode
void
startActivities(@NonNull Intent[] p0)
void
startActivities(@NonNull Intent[] p0, @Nullable Bundle p1)
void
void
void

This method is deprecated. Deprecated in Java

void
startActivityFromChild(
    @NonNull Activity p0,
    @NonNull Intent p1,
    int p2,
    @Nullable Bundle p3
)

This method is deprecated. Deprecated in Java

void

This method is deprecated. Deprecated in Java

void
startActivityFromFragment(
    @NonNull Fragment p0,
    @NonNull Intent p1,
    int p2,
    @Nullable Bundle p3
)

This method is deprecated. Deprecated in Java

boolean
boolean
void
startIntentSender(
    @NonNull IntentSender p0,
    @Nullable Intent p1,
    int p2,
    int p3,
    int p4
)
void
startIntentSender(
    @NonNull IntentSender p0,
    @Nullable Intent p1,
    int p2,
    int p3,
    int p4,
    @Nullable Bundle p5
)
void
startIntentSenderFromChild(
    @NonNull Activity p0,
    @NonNull IntentSender p1,
    int p2,
    @NonNull Intent p3,
    int p4,
    int p5,
    int p6
)

This method is deprecated. Deprecated in Java

void
startIntentSenderFromChild(
    @NonNull Activity p0,
    @NonNull IntentSender p1,
    int p2,
    @NonNull Intent p3,
    int p4,
    int p5,
    int p6,
    @Nullable Bundle p7
)

This method is deprecated. Deprecated in Java

void
void
void

This method is deprecated. Deprecated in Java

boolean
boolean
void
void
startSearch(@Nullable String p0, boolean p1, @Nullable Bundle p2, boolean p3)
void
void
void

This method is deprecated. Deprecated in Java

void
takeKeyEvents(boolean p0)
void
void
void
void
void
From android.content.Context
boolean
bindIsolatedService(
    @NonNull Intent service,
    @NonNull Context.BindServiceFlags flags,
    @NonNull String instanceName,
    @NonNull Executor executor,
    @NonNull ServiceConnection conn
)
final int
getColor(int id)
final @NonNull ColorStateList
final @Nullable Drawable
getDrawable(int id)
final @NonNull String
getString(int resId)
final @NonNull String
getString(int resId, @NonNull Object formatArgs)
final @NonNull T
<T extends Object> getSystemService(@NonNull Class<@NonNull T> serviceClass)
final @NonNull CharSequence
getText(int resId)
final @NonNull TypedArray
final @NonNull TypedArray
obtainStyledAttributes(int resid, @NonNull int[] attrs)
final @NonNull TypedArray
final @NonNull TypedArray
obtainStyledAttributes(
    @Nullable AttributeSet set,
    @NonNull int[] attrs,
    int defStyleAttr,
    int defStyleRes
)
void
void
sendBroadcastWithMultiplePermissions(
    @NonNull Intent intent,
    @NonNull String[] receiverPermissions
)
From android.view.ContextThemeWrapper
From android.content.ContextWrapper
boolean
bindIsolatedService(
    @NonNull Intent p0,
    int p1,
    @NonNull String p2,
    @NonNull Executor p3,
    @NonNull ServiceConnection p4
)
boolean
boolean
boolean
boolean
bindService(
    @NonNull Intent p0,
    int p1,
    @NonNull Executor p2,
    @NonNull ServiceConnection p3
)
boolean
boolean
bindServiceAsUser(
    @NonNull Intent p0,
    @NonNull ServiceConnection p1,
    int p2,
    @NonNull UserHandle p3
)
int
int
@NonNull int[]
int
int
@NonNull int[]
int
checkPermission(@NonNull String p0, int p1, int p2)
int
int
checkUriPermission(@NonNull Uri p0, int p1, int p2, int p3)
int
checkUriPermission(
    @Nullable Uri p0,
    @Nullable String p1,
    @Nullable String p2,
    int p3,
    int p4,
    int p5
)
@NonNull int[]
checkUriPermissions(@NonNull List<@NonNull Uri> p0, int p1, int p2, int p3)
void

This method is deprecated.

@NonNull Context
@NonNull Context
@NonNull Context
@NonNull Context
@NonNull Context
@NonNull Context
@NonNull Context
@NonNull Context
@NonNull Context
@NonNull Context
@NonNull String[]
boolean
boolean
boolean
void
void
enforceCallingOrSelfUriPermission(
    @NonNull Uri p0,
    int p1,
    @NonNull String p2
)
void
void
void
enforcePermission(@NonNull String p0, int p1, int p2, @Nullable String p3)
void
enforceUriPermission(
    @NonNull Uri p0,
    int p1,
    int p2,
    int p3,
    @NonNull String p4
)
void
enforceUriPermission(
    @Nullable Uri p0,
    @Nullable String p1,
    @Nullable String p2,
    int p3,
    int p4,
    int p5,
    @Nullable String p6
)
@NonNull String[]
@NonNull Context
@NonNull ApplicationInfo
@NonNull AttributionSource
@Nullable String
@NonNull Context
@NonNull File
@NonNull ClassLoader
@NonNull File
@NonNull ContentResolver
@NonNull File
@NonNull File
int
@NonNull File
getDir(@NonNull String p0, int p1)
@Nullable Display
@Nullable File
@NonNull File[]
@Nullable File
@NonNull File[]
@NonNull File[]

This method is deprecated.

@NonNull File
@NonNull File
@NonNull Executor
@NonNull Looper
@NonNull File
@NonNull File
@NonNull File[]
@NonNull String
@NonNull String
@NonNull PackageManager
@NonNull String
@NonNull String
@Nullable ContextParams
@NonNull SharedPreferences
String
@NonNull Drawable

This method is deprecated.

int

This method is deprecated.

int

This method is deprecated.

void
boolean
boolean
boolean
boolean
boolean
@NonNull FileInputStream
@NonNull FileOutputStream
openFileOutput(@NonNull String p0, int p1)
@NonNull SQLiteDatabase
openOrCreateDatabase(
    @NonNull String p0,
    int p1,
    @NonNull SQLiteDatabase.CursorFactory p2
)
@NonNull SQLiteDatabase
@NonNull Drawable

This method is deprecated.

void
Intent
Intent
registerReceiver(
    @Nullable BroadcastReceiver p0,
    @NonNull IntentFilter p1,
    int p2
)
Intent
registerReceiver(
    @Nullable BroadcastReceiver p0,
    @NonNull IntentFilter p1,
    @Nullable String p2,
    @Nullable Handler p3
)
Intent
registerReceiver(
    @Nullable BroadcastReceiver p0,
    @NonNull IntentFilter p1,
    @Nullable String p2,
    @Nullable Handler p3,
    int p4
)
void

This method is deprecated.

void

This method is deprecated.

void
void
void
void
void
void
void
void
void
void
sendOrderedBroadcast(
    @NonNull Intent p0,
    @Nullable String p1,
    @Nullable Bundle p2
)
void
sendOrderedBroadcast(
    @NonNull Intent p0,
    @Nullable String p1,
    @Nullable BroadcastReceiver p2,
    @Nullable Handler p3,
    int p4,
    @Nullable String p5,
    @Nullable Bundle p6
)
void
sendOrderedBroadcast(
    @NonNull Intent p0,
    @Nullable String p1,
    @Nullable Bundle p2,
    @Nullable BroadcastReceiver p3,
    @Nullable Handler p4,
    int p5,
    @Nullable String p6,
    @Nullable Bundle p7
)
void
sendOrderedBroadcast(
    @NonNull Intent p0,
    @Nullable String p1,
    @Nullable String p2,
    @Nullable BroadcastReceiver p3,
    @Nullable Handler p4,
    int p5,
    @Nullable String p6,
    @Nullable Bundle p7
)
void
sendOrderedBroadcast(
    @NonNull Intent p0,
    int p1,
    @Nullable String p2,
    @Nullable String p3,
    @Nullable BroadcastReceiver p4,
    @Nullable Handler p5,
    @Nullable String p6,
    @Nullable Bundle p7,
    @Nullable Bundle p8
)
void
sendOrderedBroadcastAsUser(
    @NonNull Intent p0,
    @NonNull UserHandle p1,
    @Nullable String p2,
    @Nullable BroadcastReceiver p3,
    @Nullable Handler p4,
    int p5,
    @Nullable String p6,
    @Nullable Bundle p7
)
void

This method is deprecated.

void

This method is deprecated.

void

This method is deprecated.

void
sendStickyOrderedBroadcast(
    @NonNull Intent p0,
    @Nullable BroadcastReceiver p1,
    @Nullable Handler p2,
    int p3,
    @Nullable String p4,
    @Nullable Bundle p5
)

This method is deprecated.

void
sendStickyOrderedBroadcastAsUser(
    @NonNull Intent p0,
    @NonNull UserHandle p1,
    @Nullable BroadcastReceiver p2,
    @Nullable Handler p3,
    int p4,
    @Nullable String p5,
    @Nullable Bundle p6
)

This method is deprecated.

void

This method is deprecated.

void

This method is deprecated.

@Nullable ComponentName
boolean
startInstrumentation(
    @NonNull ComponentName p0,
    @Nullable String p1,
    @Nullable Bundle p2
)
@Nullable ComponentName
boolean
void
void
void
void
updateServiceGroup(@NonNull ServiceConnection p0, int p1, int p2)
From android.view.Window.Callback
void

Public constructors

ComponentActivity

Added in 1.0.0
public ComponentActivity()

ComponentActivity

Added in 1.0.0
@ContentView
public 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).

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 android.app.AppComponentFactory.

Public methods

addContentView

public void addContentView(View view, ViewGroup.LayoutParams params)

addMenuProvider

Added in 1.4.0
public void addMenuProvider(@NonNull MenuProvider provider)

Adds the given MenuProvider to this MenuHost. If using this method, you must manually remove the provider when necessary.

Parameters
@NonNull MenuProvider provider

the MenuProvider to be added

addMenuProvider

Added in 1.4.0
public void addMenuProvider(
    @NonNull MenuProvider provider,
    @NonNull LifecycleOwner owner
)

Adds the given MenuProvider to this MenuHost. This MenuProvider will be removed once the given LifecycleOwner receives an Lifecycle.Event.ON_DESTROY event.

Parameters
@NonNull MenuProvider provider

the MenuProvider to be added

@NonNull LifecycleOwner owner

the Lifecycle owner whose state will determine the removal of the provider

addMenuProvider

Added in 1.4.0
public 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. This MenuProvider will be removed once the given LifecycleOwner goes down from the given Lifecycle.State.

Parameters
@NonNull MenuProvider provider

the MenuProvider to be added

@NonNull LifecycleOwner owner

the Lifecycle owner whose state will be used for automated addition/removal

@NonNull Lifecycle.State state

the Lifecycle.State to check for automated addition/removal

addOnConfigurationChangedListener

Added in 1.5.0
public final void addOnConfigurationChangedListener(
    @NonNull Consumer<@NonNull Configuration> listener
)

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

Parameters
@NonNull Consumer<@NonNull Configuration> listener

The listener that should be called whenever {ComponentCallbacks.onConfigurationChanged was called.

addOnContextAvailableListener

Added in 1.9.0-alpha03
public final void addOnContextAvailableListener(
    @NonNull OnContextAvailableListener listener
)

{@inheritDoc}

Any listener added here will receive a callback as part of super.onCreate(), but importantly before any other logic is done (including calling through to the framework Activity.onCreate with the exception of restoring the state of the savedStateRegistry for use in your listener.

addOnMultiWindowModeChangedListener

Added in 1.5.0
public final void addOnMultiWindowModeChangedListener(
    @NonNull Consumer<@NonNull MultiWindowModeChangedInfo> listener
)

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

Parameters
@NonNull Consumer<@NonNull MultiWindowModeChangedInfo> listener

The listener that should be called whenever Activity#onMultiWindowModeChanged was called.

addOnNewIntentListener

Added in 1.5.0
public final void addOnNewIntentListener(@NonNull Consumer<@NonNull Intent> listener)

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

Parameters
@NonNull Consumer<@NonNull Intent> listener

The listener that should be called whenever android.app.Activity#onNewIntent was called.

addOnPictureInPictureModeChangedListener

Added in 1.5.0
public final void addOnPictureInPictureModeChangedListener(
    @NonNull Consumer<@NonNull PictureInPictureModeChangedInfo> listener
)

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

Parameters
@NonNull Consumer<@NonNull PictureInPictureModeChangedInfo> listener

The listener that should be called whenever Activity.onPictureInPictureModeChanged was called.

addOnTrimMemoryListener

Added in 1.5.0
public final void addOnTrimMemoryListener(@NonNull Consumer<@NonNull Integer> listener)

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

Parameters
@NonNull Consumer<@NonNull Integer> listener

The listener that should be called whenever ComponentCallbacks2.onTrimMemory was called.

addOnUserLeaveHintListener

Added in 1.9.0-alpha03
public final void addOnUserLeaveHintListener(@NonNull Runnable listener)

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

Parameters
@NonNull Runnable listener

The listener that should be called whenever Activity.onUserLeaveHint was called.

dispatchKeyEvent

public boolean dispatchKeyEvent(@NonNull KeyEvent event)

dispatchKeyShortcutEvent

public boolean dispatchKeyShortcutEvent(@NonNull KeyEvent event)

getActivityResultRegistry

Added in 1.9.0-alpha03
public final @NonNull ActivityResultRegistry getActivityResultRegistry()

Get the ActivityResultRegistry associated with this activity.

getDefaultViewModelCreationExtras

@CallSuper
public @NonNull CreationExtras getDefaultViewModelCreationExtras()

{@inheritDoc}

The extras of getIntent when this is first called will be used as the defaults to any androidx.lifecycle.SavedStateHandle passed to a view model created using this extra.

getDefaultViewModelProviderFactory

Added in 1.1.0
public @NonNull ViewModelProvider.Factory getDefaultViewModelProviderFactory()

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

getFullyDrawnReporter

Added in 1.9.0-alpha03
public @NonNull FullyDrawnReporter getFullyDrawnReporter()

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

getLastCustomNonConfigurationInstance

Added in 1.0.0
Deprecated in 1.0.0
public Object getLastCustomNonConfigurationInstance()

Return the value previously returned from onRetainCustomNonConfigurationInstance.

getLifecycle

Added in 1.0.0
public @NonNull Lifecycle getLifecycle()

Returns the Lifecycle of the provider.

Returns
@NonNull Lifecycle

The lifecycle of the provider.

getOnBackPressedDispatcher

Added in 1.9.0-alpha03
public final @NonNull OnBackPressedDispatcher getOnBackPressedDispatcher()

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

Returns
@NonNull OnBackPressedDispatcher

The OnBackPressedDispatcher associated with this ComponentActivity.

getSavedStateRegistry

Added in 1.0.0
public final @NonNull SavedStateRegistry getSavedStateRegistry()

The SavedStateRegistry owned by this SavedStateRegistryOwner

getViewModelStore

Added in 1.0.0
public @NonNull 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.

Throws
kotlin.IllegalStateException

if called before the Activity is attached to the Application instance i.e., before onCreate()

initializeViewTreeOwners

Added in 1.8.0
@CallSuper
public void initializeViewTreeOwners()

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

invalidateMenu

Added in 1.4.0
public void invalidateMenu()

Invalidates the android.view.Menu to ensure that what is displayed matches the current internal state of the menu. This should be called whenever the state of the menu is changed, such as items being removed or disabled based on some user event.

onBackPressed

Added in 1.6.0
@MainThread
@CallSuper
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 android.app.Activity.onBackPressed is invoked.

onConfigurationChanged

@CallSuper
public void onConfigurationChanged(@NonNull Configuration newConfig)

{@inheritDoc}

Dispatches this call to all listeners added via addOnConfigurationChangedListener.

onCreatePanelMenu

public boolean onCreatePanelMenu(int featureId, @NonNull Menu menu)

onMenuItemSelected

public boolean onMenuItemSelected(int featureId, @NonNull MenuItem item)

onMultiWindowModeChanged

Added in 1.5.0
@CallSuper
public void onMultiWindowModeChanged(boolean isInMultiWindowMode)

{@inheritDoc}

Dispatches this call to all listeners added via addOnMultiWindowModeChangedListener.

onMultiWindowModeChanged

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

{@inheritDoc}

Dispatches this call to all listeners added via addOnMultiWindowModeChangedListener.

onPanelClosed

public void onPanelClosed(int featureId, @NonNull Menu menu)

onPictureInPictureModeChanged

Added in 1.5.0
@CallSuper
public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode)

{@inheritDoc}

Dispatches this call to all listeners added via addOnPictureInPictureModeChangedListener.

onPictureInPictureModeChanged

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

{@inheritDoc}

Dispatches this call to all listeners added via addOnPictureInPictureModeChangedListener.

onPreparePanel

public boolean onPreparePanel(int featureId, View view, @NonNull Menu menu)

onRequestPermissionsResult

Added in 1.2.0
Deprecated in 1.2.0
@CallSuper
public void onRequestPermissionsResult(
    int requestCode,
    @NonNull String[] permissions,
    @NonNull int[] grantResults
)

{@inheritDoc}

onRetainCustomNonConfigurationInstance

Added in 1.0.0
Deprecated in 1.0.0
public Object onRetainCustomNonConfigurationInstance()

Use this instead of onRetainNonConfigurationInstance. Retrieve later with lastCustomNonConfigurationInstance.

onRetainNonConfigurationInstance

Added in 1.0.0
public final Object onRetainNonConfigurationInstance()

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.

onTrimMemory

@CallSuper
public void onTrimMemory(int level)

{@inheritDoc}

Dispatches this call to all listeners added via addOnTrimMemoryListener.

peekAvailableContext

Added in 1.9.0-alpha03
public Context peekAvailableContext()

Get the Context if it is currently available. If this returns null, you can use addOnContextAvailableListener to receive a callback for when it available.

Returns
Context

the Context if it is currently available.

registerForActivityResult

Added in 1.9.0-alpha03
public 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.

This creates a record in the registry associated with this caller, managing request code, as well as conversions to/from Intent under the hood.

This must be called unconditionally, as part of initialization path, typically as a field initializer of an Activity or Fragment.

Parameters
<I extends Object>

the type of the input(if any) required to call the activity

<O extends Object>

the type of output returned as an activity result

@NonNull ActivityResultContract<@NonNull I, @NonNull O> contract

the contract, specifying conversions to/from Intents

@NonNull ActivityResultCallback<@NonNull O> callback

the callback to be called on the main thread when activity result is available

Returns
@NonNull ActivityResultLauncher<@NonNull I>

the launcher that can be used to start the activity or dispose of the prepared call.

registerForActivityResult

Added in 1.9.0-alpha03
public 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.

This creates a record in the given registry, managing request code, as well as conversions to/from Intent under the hood.

This must be called unconditionally, as part of initialization path, typically as a field initializer of an Activity or Fragment.

Parameters
<I extends Object>

the type of the input(if any) required to call the activity

<O extends Object>

the type of output returned as an activity result

@NonNull ActivityResultContract<@NonNull I, @NonNull O> contract

the contract, specifying conversions to/from Intents

@NonNull ActivityResultRegistry registry

the registry where to hold the record.

@NonNull ActivityResultCallback<@NonNull O> callback

the callback to be called on the main thread when activity result is available

Returns
@NonNull ActivityResultLauncher<@NonNull I>

the launcher that can be used to start the activity or dispose of the prepared call.

removeMenuProvider

Added in 1.4.0
public void removeMenuProvider(@NonNull MenuProvider provider)

Removes the given MenuProvider from this MenuHost.

Parameters
@NonNull MenuProvider provider

the MenuProvider to be removed

removeOnConfigurationChangedListener

Added in 1.5.0
public final void removeOnConfigurationChangedListener(
    @NonNull Consumer<@NonNull Configuration> listener
)

Remove a previously added listener. It will not receive any future callbacks.

Parameters
@NonNull Consumer<@NonNull Configuration> listener

The listener previously added with addOnConfigurationChangedListener that should be removed.

removeOnContextAvailableListener

Added in 1.9.0-alpha03
public final void removeOnContextAvailableListener(
    @NonNull OnContextAvailableListener listener
)

Remove a OnContextAvailableListener previously added via addOnContextAvailableListener.

Parameters
@NonNull OnContextAvailableListener listener

The listener that should be removed.

removeOnMultiWindowModeChangedListener

Added in 1.5.0
public final void removeOnMultiWindowModeChangedListener(
    @NonNull Consumer<@NonNull MultiWindowModeChangedInfo> listener
)

Remove a previously added listener. It will not receive any future callbacks.

Parameters
@NonNull Consumer<@NonNull MultiWindowModeChangedInfo> listener

The listener previously added with addOnMultiWindowModeChangedListener that should be removed.

removeOnNewIntentListener

Added in 1.5.0
public final void removeOnNewIntentListener(@NonNull Consumer<@NonNull Intent> listener)

Remove a previously added listener. It will not receive any future callbacks.

Parameters
@NonNull Consumer<@NonNull Intent> listener

The listener previously added with addOnNewIntentListener that should be removed.

removeOnPictureInPictureModeChangedListener

Added in 1.5.0
public final void removeOnPictureInPictureModeChangedListener(
    @NonNull Consumer<@NonNull PictureInPictureModeChangedInfo> listener
)

Remove a previously added listener. It will not receive any future callbacks.

Parameters
@NonNull Consumer<@NonNull PictureInPictureModeChangedInfo> listener

The listener previously added with addOnPictureInPictureModeChangedListener that should be removed.

removeOnTrimMemoryListener

Added in 1.5.0
public final void removeOnTrimMemoryListener(@NonNull Consumer<@NonNull Integer> listener)

Remove a previously added listener. It will not receive any future callbacks.

Parameters
@NonNull Consumer<@NonNull Integer> listener

The listener previously added with .addOnTrimMemoryListener that should be removed.

removeOnUserLeaveHintListener

Added in 1.9.0-alpha03
public final void removeOnUserLeaveHintListener(@NonNull Runnable listener)

Remove a previously added listener. It will not receive any future callbacks.

Parameters
@NonNull Runnable listener

The listener previously added with addOnUserLeaveHintListener that should be removed.

reportFullyDrawn

public void reportFullyDrawn()

setContentView

public void setContentView(@LayoutRes int layoutResID)

setContentView

public void setContentView(View view)

setContentView

public void setContentView(View view, ViewGroup.LayoutParams params)

startActivityForResult

Added in 1.2.0
Deprecated in 1.2.0
public void startActivityForResult(@NonNull Intent intent, int requestCode)

{@inheritDoc}

startActivityForResult

Added in 1.2.0
Deprecated in 1.2.0
public void startActivityForResult(
    @NonNull Intent intent,
    int requestCode,
    Bundle options
)

{@inheritDoc}

startIntentSenderForResult

Added in 1.2.0
Deprecated in 1.2.0
public void startIntentSenderForResult(
    @NonNull IntentSender intent,
    int requestCode,
    Intent fillInIntent,
    int flagsMask,
    int flagsValues,
    int extraFlags
)

{@inheritDoc}

startIntentSenderForResult

Added in 1.2.0
Deprecated in 1.2.0
public void startIntentSenderForResult(
    @NonNull IntentSender intent,
    int requestCode,
    Intent fillInIntent,
    int flagsMask,
    int flagsValues,
    int extraFlags,
    Bundle options
)

{@inheritDoc}

Protected methods

onActivityResult

Added in 1.2.0
Deprecated in 1.2.0
@CallSuper
protected void onActivityResult(int requestCode, int resultCode, Intent data)

{@inheritDoc}

onCreate

protected void onCreate(Bundle savedInstanceState)

{@inheritDoc}

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

onNewIntent

@CallSuper
protected void onNewIntent(@NonNull Intent intent)

{@inheritDoc}

Dispatches this call to all listeners added via addOnNewIntentListener.

onSaveInstanceState

@CallSuper
protected void onSaveInstanceState(@NonNull Bundle outState)

onUserLeaveHint

@CallSuper
protected void onUserLeaveHint()

{@inheritDoc}

Dispatches this call to all listeners added via addOnUserLeaveHintListener.

shouldDumpInternalState

protected final boolean shouldDumpInternalState(String[] args)

Checks if the internal state should be dump, as some special args are handled by Activity itself.

Subclasses implementing Activity.dump should typically start with:

override fun dump(
prefix: String,
fd: FileDescriptor?,
writer: PrintWriter,
args: Array<out String>?
) {
super.dump(prefix, fd, writer, args)

if (!shouldDumpInternalState(args)) {
return
}
// dump internal state
}

Extension functions

ActivityViewModelLazyKt.viewModels

@MainThread
public final @NonNull Lazy<@NonNull VM> <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.

class MyComponentActivity : ComponentActivity() {
val viewmodel: MyViewModel by viewModels()
}

This property can be accessed only after the Activity is attached to the Application, and access prior to that will result in IllegalArgumentException.

EdgeToEdge.enableEdgeToEdge

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

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

To set it up with the default style, call this method in your Activity's onCreate method:

    override fun onCreate(savedInstanceState: Bundle?) {
enableEdgeToEdge()
super.onCreate(savedInstanceState)
...
}

The default style configures the system bars with a transparent background when contrast can be enforced by the system (API 29 or above). On older platforms (which only have 3-button/2-button navigation modes), an equivalent scrim is applied to ensure contrast with the system bars.

See SystemBarStyle for more customization options.

Parameters
@NonNull SystemBarStyle statusBarStyle

The SystemBarStyle for the status bar.

@NonNull SystemBarStyle navigationBarStyle

The SystemBarStyle for the navigation bar.