AppCompatActivity
class AppCompatActivity : FragmentActivity, AppCompatCallback, TaskStackBuilder.SupportParentable, ActionBarDrawerToggle.DelegateProvider
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
setSupportActionBarAPI. - Built-in switching between light and dark themes by using the
Theme.AppCompat.DayNighttheme andsetDefaultNightModeAPI. - Integration with
DrawerLayoutby using thegetDrawerToggleDelegateAPI.
Note that every activity that extends this class has to be themed with Theme.AppCompat or a theme that extends that theme.
Summary
Public constructors |
|---|
|
Default constructor for AppCompatActivity. |
@ContentViewAlternate constructor that can be used to provide a default layout that will be inflated as part of |
Public functions |
|
|---|---|
Unit |
addContentView(view: View!, params: ViewGroup.LayoutParams!) |
Unit |
|
Boolean |
dispatchKeyEvent(event: KeyEvent!) |
T! |
<T : View?> findViewById(id: @IdRes Int) |
AppCompatDelegate |
|
ActionBarDrawerToggle.Delegate? |
|
MenuInflater |
|
Resources! |
|
ActionBar? |
Support library version of |
Intent? |
Obtain an |
Unit |
|
Unit |
onConfigurationChanged(newConfig: Configuration){@inheritDoc} |
Unit |
|
Unit |
Support version of |
Boolean |
|
Boolean |
onMenuItemSelected(featureId: Int, item: MenuItem) |
Boolean |
onMenuOpened(featureId: Int, menu: Menu!)Please note: AppCompat uses its own feature id for the action bar: |
Unit |
onPanelClosed(featureId: Int, menu: Menu)Please note: AppCompat uses its own feature id for the action bar: |
Unit |
Support version of |
Unit |
Notifies the activity that a support action mode has finished. |
Unit |
Notifies the Activity that a support action mode has been started. |
Unit |
This function is deprecated. Use onContentChanged instead. |
Boolean |
This method is called whenever the user chooses to navigate Up within your application's activity hierarchy from the action bar. |
ActionMode? |
Called when a support action mode is being started for this window. |
Unit |
|
Unit |
setContentView(layoutResID: @LayoutRes Int) |
Unit |
setContentView(view: View!) |
Unit |
setContentView(view: View!, params: ViewGroup.LayoutParams!) |
Unit |
setSupportActionBar(toolbar: Toolbar?)Set a |
Unit |
This function is deprecated. Progress bars are no longer provided in AppCompat. |
Unit |
This function is deprecated. Progress bars are no longer provided in AppCompat. |
Unit |
This function is deprecated. Progress bars are no longer provided in AppCompat. |
Unit |
This function is deprecated. Progress bars are no longer provided in AppCompat. |
Unit |
|
ActionMode? |
startSupportActionMode(callback: ActionMode.Callback)Start an action mode. |
Unit |
This function is deprecated. Call |
Unit |
supportNavigateUpTo(upIntent: Intent)Navigate from sourceActivity to the activity specified by upIntent, finishing sourceActivity in the process. upIntent will have the flag |
Boolean |
supportRequestWindowFeature(featureId: Int)Enable extended support library window features. |
Boolean |
supportShouldUpRecreateTask(targetIntent: Intent)Returns true if sourceActivity should recreate the task when navigating 'up' by using targetIntent. |
Protected functions |
|
|---|---|
Unit |
attachBaseContext(newBase: Context!) |
Unit |
Destroy all fragments. |
Unit |
onLocalesChanged(locales: LocaleListCompat)Called when the locales have been changed. |
Unit |
onNightModeChanged(mode: Int)Called when the night mode has changed. |
Unit |
onPostCreate(savedInstanceState: Bundle?) |
Unit |
Dispatch onResume() to fragments. |
Unit |
onStart()Dispatch onStart() to all fragments. |
Unit |
onStop()Dispatch onStop() to all fragments. |
Unit |
onTitleChanged(title: CharSequence!, color: Int) |
Extension functions |
|
|---|---|
Unit |
AppCompatActivity.setupActionBarWithNavController(Sets up the ActionBar returned by |
Unit |
AppCompatActivity.setupActionBarWithNavController(Sets up the ActionBar returned by |
Inherited Constants |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Inherited properties |
||
|---|---|---|
|
Public constructors
AppCompatActivity
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.
AppCompatActivity
@ContentView
AppCompatActivity(contentLayoutId: @LayoutRes 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 | |
|---|---|
AppCompatActivity |
Public functions
getDelegate
fun getDelegate(): AppCompatDelegate
| Returns | |
|---|---|
AppCompatDelegate |
The |
getDrawerToggleDelegate
fun getDrawerToggleDelegate(): ActionBarDrawerToggle.Delegate?
| Returns | |
|---|---|
ActionBarDrawerToggle.Delegate? |
Delegate to use for ActionBarDrawableToggles, or null if the Activity does not wish to override the default behavior. |
getSupportActionBar
fun getSupportActionBar(): ActionBar?
Support library version of getActionBar.
Retrieve a reference to this activity's ActionBar.
| Returns | |
|---|---|
ActionBar? |
The Activity's ActionBar, or null if it does not have one. |
getSupportParentActivityIntent
fun getSupportParentActivityIntent(): Intent?
Obtain an android.content.Intent that will launch an explicit target activity specified by sourceActivity's PARENT_ACTIVITY
| Returns | |
|---|---|
Intent? |
a new Intent targeting the defined parent activity of sourceActivity |
onConfigurationChanged
fun onConfigurationChanged(newConfig: Configuration): Unit
{@inheritDoc}
Dispatches this call to all listeners added via addOnConfigurationChangedListener.
onCreateSupportNavigateUpTaskStack
fun onCreateSupportNavigateUpTaskStack(builder: TaskStackBuilder): Unit
Support version of onCreateNavigateUpTaskStack. 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 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.
| Parameters | |
|---|---|
builder: TaskStackBuilder |
An empty TaskStackBuilder - the application should add intents representing the desired task stack |
onMenuOpened
fun onMenuOpened(featureId: Int, menu: Menu!): Boolean
Please note: AppCompat uses its own feature id for the action bar: FEATURE_SUPPORT_ACTION_BAR.
onPanelClosed
fun onPanelClosed(featureId: Int, menu: Menu): Unit
Please note: AppCompat uses its own feature id for the action bar: FEATURE_SUPPORT_ACTION_BAR.
onPrepareSupportNavigateUpTaskStack
fun onPrepareSupportNavigateUpTaskStack(builder: TaskStackBuilder): Unit
Support version of onPrepareNavigateUpTaskStack. 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. 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
fun onSupportActionModeFinished(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
fun onSupportActionModeStarted(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. |
onSupportNavigateUp
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 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, and supportNavigateUpTo for help implementing custom Up navigation.
| Returns | |
|---|---|
Boolean |
true if Up navigation completed successfully and this Activity was finished, false otherwise. |
onWindowStartingSupportActionMode
fun onWindowStartingSupportActionMode(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 |
| Returns | |
|---|---|
ActionMode? |
The ActionMode that was started, or null if the system should present it |
setSupportActionBar
fun setSupportActionBar(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 |
startSupportActionMode
fun startSupportActionMode(callback: ActionMode.Callback): ActionMode?
Start an action mode.
| Parameters | |
|---|---|
callback: ActionMode.Callback |
Callback that will manage lifecycle events for this context mode |
| Returns | |
|---|---|
ActionMode? |
The ContextMode that was started, or null if it was canceled |
supportInvalidateOptionsMenu
fun supportInvalidateOptionsMenu(): Unit
Support library version of invalidateOptionsMenu.
Invalidate the activity's options menu. This will cause relevant presentations of the menu to fully update via calls to onCreateOptionsMenu and onPrepareOptionsMenu the next time the menu is requested.
supportNavigateUpTo
fun supportNavigateUpTo(upIntent: Intent): Unit
Navigate from sourceActivity to the activity specified by upIntent, finishing sourceActivity in the process. upIntent will have the flag 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.
| Parameters | |
|---|---|
upIntent: Intent |
An intent representing the target destination for up navigation |
supportRequestWindowFeature
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 |
| Returns | |
|---|---|
Boolean |
Returns true if the requested feature is supported and now enabled. |
| See also | |
|---|---|
requestWindowFeature |
|
requestFeature |
supportShouldUpRecreateTask
fun supportShouldUpRecreateTask(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 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 |
| Returns | |
|---|---|
Boolean |
true if navigating up should recreate a new task stack, false if the same task should be used for the destination |
Protected functions
onLocalesChanged
protected fun onLocalesChanged(locales: LocaleListCompat): Unit
Called when the locales have been changed. See applyAppLocales for more information.
| Parameters | |
|---|---|
locales: LocaleListCompat |
the localeListCompat which has been applied |
onNightModeChanged
protected fun onNightModeChanged(mode: Int): Unit
Called when the night mode has changed. See applyDayNight for more information.
| Parameters | |
|---|---|
mode: Int |
the night mode which has been applied |
Extension functions
setupActionBarWithNavController
fun AppCompatActivity.setupActionBarWithNavController(
navController: NavController,
configuration: AppBarConfiguration = AppBarConfiguration(navController.graph)
): Unit
Sets up the ActionBar returned by AppCompatActivity.getSupportActionBar for use with a NavController.
By calling this method, the title in the action bar will automatically be updated when the destination changes (assuming there is a valid label).
The AppBarConfiguration you provide controls how the Navigation button is displayed.
You are responsible for calling NavController.navigateUp to handle the Navigation button. Typically this is done in AppCompatActivity.onSupportNavigateUp.
| Parameters | |
|---|---|
navController: NavController |
The NavController whose navigation actions will be reflected in the title of the action bar. |
configuration: AppBarConfiguration = AppBarConfiguration(navController.graph) |
Additional configuration options for customizing the behavior of the ActionBar |
setupActionBarWithNavController
fun AppCompatActivity.setupActionBarWithNavController(
navController: NavController,
drawerLayout: DrawerLayout?
): Unit
Sets up the ActionBar returned by AppCompatActivity.getSupportActionBar for use with a NavController.
By calling this method, the title in the action bar will automatically be updated when the destination changes (assuming there is a valid label).
The start destination of your navigation graph is considered the only top level destination. On the start destination of your navigation graph, the ActionBar will show the drawer icon if the given drawerLayout is non null. On all other destinations, the ActionBar will show the Up button.
You are responsible for calling NavController.navigateUp to handle the Navigation button. Typically this is done in AppCompatActivity.onSupportNavigateUp.
| Parameters | |
|---|---|
navController: NavController |
The NavController whose navigation actions will be reflected in the title of the action bar. |
drawerLayout: DrawerLayout? |
The DrawerLayout that should be toggled from the Navigation button |