Save the date! Android Dev Summit is coming to Sunnyvale, CA on Oct 23-24, 2019.

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 use the support library action bar features.

You can add an androidx.appcompat.app.ActionBar to your activity when running on API level 7 or higher by extending this class for your activity and setting the activity theme to androidx.appcompat.R.style#Theme_AppCompat or a similar theme.

Developer Guides

For information about how to use the action bar, including how to add action items, navigation modes and more, read the Action Bar API guide.

Summary

Public constructors

Default constructor for AppCompatActivity.

<init>(@LayoutRes contentLayoutId: Int)

Alternate constructor that can be used to provide a default layout that will be inflated as part of super.onCreate(savedInstanceState).

Public methods
open Boolean
onKeyDown(keyCode: Int, event: KeyEvent!)

open T
findViewById(@IdRes id: Int)

open Unit
setSupportActionBar(@Nullable toolbar: Toolbar?)

Set a Toolbar to act as the androidx.appcompat.app.ActionBar for this Activity window.

open 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.

open MenuInflater

open Unit

open Unit

open Unit

Notifies the Activity that a support action mode has been started.

open Resources!

open Unit
setTheme(@StyleRes resId: Int)

open Unit
addContentView(view: View!, params: LayoutParams!)

open Boolean

Enable extended support library window features.

open ActionBarDrawerToggle.Delegate?

open Boolean

This method is called whenever the user chooses to navigate Up within your application's activity hierarchy from the action bar.

open Unit
setContentView(@LayoutRes layoutResID: Int)

open Unit

open Unit
setContentView(view: View!, params: LayoutParams!)

open Boolean
onMenuOpened(featureId: Int, menu: Menu!)

Please note: AppCompat uses its own feature id for the action bar: FEATURE_SUPPORT_ACTION_BAR.

open Unit
onPanelClosed(featureId: Int, @NonNull menu: Menu)

Call onOptionsMenuClosed() on fragments.

open Unit

Notifies the activity that a support action mode has finished.

open ActionBar?

Support library version of android.app.Activity#getActionBar.

open Unit

open Unit

open Boolean
supportShouldUpRecreateTask(@NonNull targetIntent: Intent)

Returns true if sourceActivity should recreate the task when navigating 'up' by using targetIntent.

open Unit

open AppCompatDelegate

open Unit

open ActionMode?

Called when a support action mode is being started for this window.

open ActionMode?

Start an action mode.

open Unit

open Unit
supportNavigateUpTo(@NonNull upIntent: Intent)

Navigate from sourceActivity to the activity specified by upIntent, finishing sourceActivity in the process.

open Unit

Support version of onCreateNavigateUpTaskStack(android.app.TaskStackBuilder).

open Unit

Support version of onPrepareNavigateUpTaskStack(android.app.TaskStackBuilder).

open Unit

open Unit

open Boolean

Boolean
onMenuItemSelected(featureId: Int, @NonNull item: MenuItem)

open Unit

open Unit

Protected methods
open Unit

open Unit

open Unit
onCreate(@Nullable savedInstanceState: Bundle?)

open Unit

open Unit
onTitleChanged(title: CharSequence!, color: Int)

open Unit
onSaveInstanceState(@NonNull outState: Bundle)

open Unit
onPostCreate(@Nullable savedInstanceState: Bundle?)

open Unit

Called when the night mode has changed.

open Unit

open Unit

Inherited functions
Extension functions
From androidx.navigation.ui.androidx.appcompat.app.AppCompatActivity
Unit

Sets up the ActionBar returned by AppCompatActivity.getSupportActionBar for use with a NavController.

Unit
AppCompatActivity.setupActionBarWithNavController(navController: NavController, configuration: AppBarConfiguration = AppBarConfiguration(navController.graph))

Sets up the ActionBar returned by AppCompatActivity.getSupportActionBar for use with a NavController.

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.

Public methods

onKeyDown

open fun onKeyDown(keyCode: Int, event: KeyEvent!): Boolean

findViewById

open fun <T : View!> findViewById(@IdRes id: Int): T

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

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

getMenuInflater

@NonNull open fun getMenuInflater(): MenuInflater

setSupportProgress

open fun setSupportProgress(progress: Int): Unit

onSupportContentChanged

open fun onSupportContentChanged(): Unit

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.

getResources

open fun getResources(): Resources!

setTheme

open fun setTheme(@StyleRes resId: Int): Unit

addContentView

open fun addContentView(view: View!, params: LayoutParams!): Unit

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.

getDrawerToggleDelegate

@Nullable open fun getDrawerToggleDelegate(): ActionBarDrawerToggle.Delegate?

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.

setContentView

open fun setContentView(@LayoutRes layoutResID: Int): Unit

setContentView

open fun setContentView(view: View!): Unit

setContentView

open fun setContentView(view: View!, params: LayoutParams!): Unit

onMenuOpened

open fun onMenuOpened(featureId: Int, menu: 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: Menu): Unit

Call onOptionsMenuClosed() on fragments.

Please note: AppCompat uses its own feature id for the action bar: FEATURE_SUPPORT_ACTION_BAR.

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.

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.

onConfigurationChanged

open fun onConfigurationChanged(@NonNull newConfig: Configuration): Unit

supportInvalidateOptionsMenu

open fun supportInvalidateOptionsMenu(): Unit

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

setSupportProgressBarIndeterminate

open fun setSupportProgressBarIndeterminate(indeterminate: Boolean): Unit

getDelegate

@NonNull open fun getDelegate(): AppCompatDelegate
Return
AppCompatDelegate: The AppCompatDelegate being used by this Activity.

onContentChanged

open fun onContentChanged(): Unit

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

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

openOptionsMenu

open fun openOptionsMenu(): Unit

supportNavigateUpTo

open fun supportNavigateUpTo(@NonNull upIntent: 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

onCreateSupportNavigateUpTaskStack

open fun onCreateSupportNavigateUpTaskStack(@NonNull builder: 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

onPrepareSupportNavigateUpTaskStack

open fun onPrepareSupportNavigateUpTaskStack(@NonNull builder: 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.

closeOptionsMenu

open fun closeOptionsMenu(): Unit

setSupportProgressBarIndeterminateVisibility

open fun setSupportProgressBarIndeterminateVisibility(visible: Boolean): Unit

dispatchKeyEvent

open fun dispatchKeyEvent(event: KeyEvent!): Boolean

onMenuItemSelected

fun onMenuItemSelected(featureId: Int, @NonNull item: MenuItem): Boolean

invalidateOptionsMenu

open fun invalidateOptionsMenu(): Unit

setSupportProgressBarVisibility

open fun setSupportProgressBarVisibility(visible: Boolean): Unit

Protected methods

onPostResume

protected open fun onPostResume(): Unit

onDestroy

protected open fun onDestroy(): Unit

onCreate

protected open fun onCreate(@Nullable savedInstanceState: Bundle?): Unit

onStart

protected open fun onStart(): Unit

onTitleChanged

protected open fun onTitleChanged(title: CharSequence!, color: Int): Unit

onSaveInstanceState

protected open fun onSaveInstanceState(@NonNull outState: Bundle): Unit

onPostCreate

protected open fun onPostCreate(@Nullable savedInstanceState: Bundle?): 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

onStop

protected open fun onStop(): Unit

attachBaseContext

protected open fun attachBaseContext(newBase: Context!): Unit