AppCompatActivity
open class AppCompatActivity : FragmentActivity, AppCompatCallback, TaskStackBuilder.SupportParentable, ActionBarDrawerToggle.DelegateProvider
| androidx.activity.ComponentActivity | ||
| ↳ | androidx.fragment.app.FragmentActivity | |
| ↳ | androidx.appcompat.app.AppCompatActivity | |
Base class for activities that wish to use some of the newer platform features on older Android devices. Some of these backported features include:
- Using the action bar, including action items, navigation modes and more with the
setSupportActionBar(Toolbar)API. - Built-in switching between light and dark themes by using the androidx.appcompat.R.style#Theme_AppCompat_DayNight theme and
AppCompatDelegate#setDefaultNightMode(int)API. - Integration with
DrawerLayoutby using thegetDrawerToggleDelegate()API.
Note that every activity that extends this class has to be themed with androidx.appcompat.R.style#Theme_AppCompat or a theme that extends that theme.
Summary
| Public constructors | |
|---|---|
<init>()Default constructor for AppCompatActivity. |
|
|
Alternate constructor that can be used to provide a default layout that will be inflated as part of |
|
| Public methods | |
|---|---|
| open Unit |
addContentView(view: View!, params: LayoutParams!) |
| open Unit | |
| open Boolean |
dispatchKeyEvent(event: KeyEvent!) |
| open T |
findViewById(@IdRes id: Int) |
| open AppCompatDelegate | |
| open ActionBarDrawerToggle.Delegate? | |
| open MenuInflater | |
| open Resources! | |
| open ActionBar? |
Support library version of |
| open Intent? |
Obtain an |
| open Unit | |
| open Unit |
onConfigurationChanged(@NonNull newConfig: Configuration) |
| open Unit | |
| open Unit |
onCreateSupportNavigateUpTaskStack(@NonNull : TaskStackBuilder)Support version of |
| open Boolean | |
| Boolean |
onMenuItemSelected(featureId: Int, @NonNull item: MenuItem) |
| open Boolean |
onMenuOpened(featureId: Int, : Menu)Please note: AppCompat uses its own feature id for the action bar: |
| open Unit |
onPanelClosed(featureId: Int, @NonNull : Menu)Call onOptionsMenuClosed() on fragments. |
| open Unit |
onPrepareSupportNavigateUpTaskStack(@NonNull : TaskStackBuilder)Support version of |
| open Unit |
onSupportActionModeFinished(@NonNull mode: ActionMode)Notifies the activity that a support action mode has finished. |
| open Unit |
onSupportActionModeStarted(@NonNull mode: ActionMode)Notifies the Activity that a support action mode has been started. |
| open Unit | |
| open Boolean |
This method is called whenever the user chooses to navigate Up within your application's activity hierarchy from the action bar. |
| open ActionMode? |
onWindowStartingSupportActionMode(@NonNull callback: ActionMode.Callback)Called when a support action mode is being started for this window. |
| open Unit | |
| open Unit |
setContentView(@LayoutRes layoutResID: Int) |
| open Unit |
setContentView(view: View!) |
| open Unit |
setContentView(view: View!, params: LayoutParams!) |
| open Unit |
setSupportActionBar(@Nullable toolbar: Toolbar?)Set a |
| open Unit |
setSupportProgress(progress: Int) |
| open Unit |
setSupportProgressBarIndeterminate(indeterminate: Boolean) |
| open Unit | |
| open Unit |
setSupportProgressBarVisibility(visible: Boolean) |
| open Unit | |
| open ActionMode? |
startSupportActionMode(@NonNull callback: ActionMode.Callback)Start an action mode. |
| open Unit | |
| open Unit |
supportNavigateUpTo(@NonNull : Intent)Navigate from sourceActivity to the activity specified by upIntent, finishing sourceActivity in the process. |
| open Boolean |
supportRequestWindowFeature(featureId: Int)Enable extended support library window features. |
| open Boolean |
supportShouldUpRecreateTask(@NonNull targetIntent: Intent)Returns true if sourceActivity should recreate the task when navigating 'up' by using targetIntent. |
| Protected methods | |
|---|---|
| open Unit |
attachBaseContext(newBase: Context!) |
| open Unit | |
| open Unit | |
| open Unit |
onNightModeChanged(mode: Int)Called when the night mode has changed. |
| open Unit |
onPostCreate(@Nullable savedInstanceState: Bundle?) |
| open Unit | |
| open Unit |
onSaveInstanceState(@NonNull outState: Bundle) |
| open Unit |
onStart() |
| open Unit |
onStop() |
| open Unit |
onTitleChanged(title: CharSequence!, color: Int) |
| Inherited functions | |
|---|---|
| Extension functions | ||||
|---|---|---|---|---|
From androidx.navigation.ui.androidx.appcompat.app.AppCompatActivity
|
||||
Public constructors
<init>
AppCompatActivity()
Default constructor for AppCompatActivity. All Activities must have a default constructor for API 27 and lower devices or when using the default android.app.AppComponentFactory.
<init>
AppCompatActivity(@LayoutRes contentLayoutId: Int)
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.
See Also
Public methods
addContentView
open fun addContentView(
view: View!,
params: LayoutParams!
): Unit
closeOptionsMenu
open fun closeOptionsMenu(): Unit
getDelegate
@NonNull open fun getDelegate(): AppCompatDelegate
| Return | |
|---|---|
AppCompatDelegate |
The AppCompatDelegate being used by this Activity. |
getDrawerToggleDelegate
@Nullable open fun getDrawerToggleDelegate(): ActionBarDrawerToggle.Delegate?
getMenuInflater
@NonNull open fun getMenuInflater(): MenuInflater
getResources
open fun getResources(): Resources!
getSupportActionBar
@Nullable open fun getSupportActionBar(): ActionBar?
Support library version of android.app.Activity#getActionBar.
Retrieve a reference to this activity's ActionBar.
| Return | |
|---|---|
ActionBar? |
The Activity's ActionBar, or null if it does not have one. |
getSupportParentActivityIntent
@Nullable open fun getSupportParentActivityIntent(): Intent?
Obtain an android.content.Intent that will launch an explicit target activity specified by sourceActivity's androidx.core.app.NavUtils#PARENT_ACTIVITY <meta-data> element in the application's manifest. If the device is running Jellybean or newer, the android:parentActivityName attribute will be preferred if it is present.
| Return | |
|---|---|
Intent? |
a new Intent targeting the defined parent activity of sourceActivity |
invalidateOptionsMenu
open fun invalidateOptionsMenu(): Unit
onConfigurationChanged
open fun onConfigurationChanged(@NonNull newConfig: Configuration): Unit
onContentChanged
open fun onContentChanged(): Unit
onCreateSupportNavigateUpTaskStack
open fun onCreateSupportNavigateUpTaskStack(@NonNull : TaskStackBuilder): Unit
Support version of onCreateNavigateUpTaskStack(android.app.TaskStackBuilder). This method will be called on all platform versions. Define the synthetic task stack that will be generated during Up navigation from a different task.
The default implementation of this method adds the parent chain of this activity as specified in the manifest to the supplied androidx.core.app.TaskStackBuilder. Applications may choose to override this method to construct the desired task stack in a different way.
This method will be invoked by the default implementation of onNavigateUp() if shouldUpRecreateTask(android.content.Intent) returns true when supplied with the intent returned by getParentActivityIntent().
Applications that wish to supply extra Intent parameters to the parent stack defined by the manifest should override onPrepareSupportNavigateUpTaskStack(androidx.core.app.TaskStackBuilder).
| Parameters | |
|---|---|
builder |
TaskStackBuilder: An empty TaskStackBuilder - the application should add intents representing the desired task stack |
onMenuOpened
open fun onMenuOpened(
featureId: Int,
: Menu
): Boolean
Please note: AppCompat uses its own feature id for the action bar: FEATURE_SUPPORT_ACTION_BAR.
onPanelClosed
open fun onPanelClosed(
featureId: Int,
@NonNull : Menu
): Unit
Call onOptionsMenuClosed() on fragments.
Please note: AppCompat uses its own feature id for the action bar: FEATURE_SUPPORT_ACTION_BAR.
onPrepareSupportNavigateUpTaskStack
open fun onPrepareSupportNavigateUpTaskStack(@NonNull : TaskStackBuilder): Unit
Support version of onPrepareNavigateUpTaskStack(android.app.TaskStackBuilder). This method will be called on all platform versions. Prepare the synthetic task stack that will be generated during Up navigation from a different task.
This method receives the androidx.core.app.TaskStackBuilder with the constructed series of Intents as generated by onCreateSupportNavigateUpTaskStack(androidx.core.app.TaskStackBuilder). If any extra data should be added to these intents before launching the new task, the application should override this method and add that data here.
| Parameters | |
|---|---|
builder |
TaskStackBuilder: A TaskStackBuilder that has been populated with Intents by onCreateNavigateUpTaskStack. |
onSupportActionModeFinished
@CallSuper open fun onSupportActionModeFinished(@NonNull mode: ActionMode): Unit
Notifies the activity that a support action mode has finished. Activity subclasses overriding this method should call the superclass implementation.
| Parameters | |
|---|---|
mode |
ActionMode: The action mode that just finished. |
onSupportActionModeStarted
@CallSuper open fun onSupportActionModeStarted(@NonNull mode: ActionMode): Unit
Notifies the Activity that a support action mode has been started. Activity subclasses overriding this method should call the superclass implementation.
| Parameters | |
|---|---|
mode |
ActionMode: The new action mode. |
onSupportContentChanged
open funonSupportContentChanged(): Unit
Deprecated: Use #onContentChanged() instead.
onSupportNavigateUp
open fun onSupportNavigateUp(): Boolean
This method is called whenever the user chooses to navigate Up within your application's activity hierarchy from the action bar.
If a parent was specified in the manifest for this activity or an activity-alias to it, default Up navigation will be handled automatically. See getSupportParentActivityIntent() for how to specify the parent. If any activity along the parent chain requires extra Intent arguments, the Activity subclass should override the method onPrepareSupportNavigateUpTaskStack(androidx.core.app.TaskStackBuilder) to supply those arguments.
See Tasks and Back Stack from the developer guide and Navigation from the design guide for more information about navigating within your app.
See the androidx.core.app.TaskStackBuilder class and the Activity methods getSupportParentActivityIntent(), supportShouldUpRecreateTask(android.content.Intent), and supportNavigateUpTo(android.content.Intent) for help implementing custom Up navigation.
| Return | |
|---|---|
Boolean |
true if Up navigation completed successfully and this Activity was finished, false otherwise. |
onWindowStartingSupportActionMode
@Nullable open fun onWindowStartingSupportActionMode(@NonNull callback: ActionMode.Callback): ActionMode?
Called when a support action mode is being started for this window. Gives the callback an opportunity to handle the action mode in its own unique and beautiful way. If this method returns null the system can choose a way to present the mode or choose not to start the mode at all.
| Parameters | |
|---|---|
callback |
ActionMode.Callback: Callback to control the lifecycle of this action mode |
| Return | |
|---|---|
ActionMode? |
The ActionMode that was started, or null if the system should present it |
openOptionsMenu
open fun openOptionsMenu(): Unit
setContentView
open fun setContentView(
view: View!,
params: LayoutParams!
): Unit
setSupportActionBar
open fun setSupportActionBar(@Nullable toolbar: Toolbar?): Unit
Set a Toolbar to act as the androidx.appcompat.app.ActionBar for this Activity window.
When set to a non-null value the getActionBar() method will return an androidx.appcompat.app.ActionBar object that can be used to control the given toolbar as if it were a traditional window decor action bar. The toolbar's menu will be populated with the Activity's options menu and the navigation button will be wired through the standard home menu select action.
In order to use a Toolbar within the Activity's window content the application must not request the window feature FEATURE_SUPPORT_ACTION_BAR.
| Parameters | |
|---|---|
toolbar |
Toolbar?: Toolbar to set as the Activity's action bar, or null to clear it |
setSupportProgress
open funsetSupportProgress(progress: Int): Unit
Deprecated: Progress bars are no longer provided in AppCompat.
setSupportProgressBarIndeterminate
open funsetSupportProgressBarIndeterminate(indeterminate: Boolean): Unit
Deprecated: Progress bars are no longer provided in AppCompat.
setSupportProgressBarIndeterminateVisibility
open funsetSupportProgressBarIndeterminateVisibility(visible: Boolean): Unit
Deprecated: Progress bars are no longer provided in AppCompat.
setSupportProgressBarVisibility
open funsetSupportProgressBarVisibility(visible: Boolean): Unit
Deprecated: Progress bars are no longer provided in AppCompat.
startSupportActionMode
@Nullable open fun startSupportActionMode(@NonNull callback: ActionMode.Callback): ActionMode?
Start an action mode.
| Parameters | |
|---|---|
callback |
ActionMode.Callback: Callback that will manage lifecycle events for this context mode |
| Return | |
|---|---|
ActionMode? |
The ContextMode that was started, or null if it was canceled |
supportInvalidateOptionsMenu
open fun supportInvalidateOptionsMenu(): Unit
supportNavigateUpTo
open fun supportNavigateUpTo(@NonNull : Intent): Unit
Navigate from sourceActivity to the activity specified by upIntent, finishing sourceActivity in the process. upIntent will have the flag android.content.Intent#FLAG_ACTIVITY_CLEAR_TOP set by this method, along with any others required for proper up navigation as outlined in the Android Design Guide.
This method should be used when performing up navigation from within the same task as the destination. If up navigation should cross tasks in some cases, see supportShouldUpRecreateTask(android.content.Intent).
| Parameters | |
|---|---|
upIntent |
Intent: An intent representing the target destination for up navigation |
supportRequestWindowFeature
open fun supportRequestWindowFeature(featureId: Int): Boolean
Enable extended support library window features.
This is a convenience for calling getWindow().requestFeature().
| Parameters | |
|---|---|
featureId |
Int: The desired feature as defined in android.view.Window or androidx.core.view.WindowCompat. |
| Return | |
|---|---|
Boolean |
Returns true if the requested feature is supported and now enabled. |
supportShouldUpRecreateTask
open fun supportShouldUpRecreateTask(@NonNull targetIntent: Intent): Boolean
Returns true if sourceActivity should recreate the task when navigating 'up' by using targetIntent.
If this method returns false the app can trivially call supportNavigateUpTo(android.content.Intent) using the same parameters to correctly perform up navigation. If this method returns false, the app should synthesize a new task stack by using androidx.core.app.TaskStackBuilder or another similar mechanism to perform up navigation.
| Parameters | |
|---|---|
targetIntent |
Intent: An intent representing the target destination for up navigation |
| Return | |
|---|---|
Boolean |
true if navigating up should recreate a new task stack, false if the same task should be used for the destination |
Protected methods
onDestroy
protected open fun onDestroy(): Unit
onNightModeChanged
protected open fun onNightModeChanged(mode: Int): Unit
Called when the night mode has changed. See AppCompatDelegate#applyDayNight() for more information.
| Parameters | |
|---|---|
mode |
Int: the night mode which has been applied |
onPostResume
protected open fun onPostResume(): Unit
onStart
protected open fun onStart(): Unit
onStop
protected open fun onStop(): Unit
onTitleChanged
protected open fun onTitleChanged(
title: CharSequence!,
color: Int
): Unit

