Stay organized with collections
Save and categorize content based on your preferences.
SystemOnBackInvokedCallbacks
class SystemOnBackInvokedCallbacks
Utility class providing OnBackInvokedCallback
s to override the default behavior when system back is invoked. e.g. Activity.finish
By registering these callbacks with the OnBackInvokedDispatcher
, the system can trigger specific behaviors and play corresponding ahead-of-time animations when the back gesture is invoked.
For example, to trigger the Activity.moveTaskToBack
behavior:
OnBackInvokedDispatcher dispatcher = activity.getOnBackInvokedDispatcher();
dispatcher.registerOnBackInvokedCallback(
OnBackInvokedDispatcher.PRIORITY_DEFAULT,
SystemOnBackInvokedCallbacks.moveTaskToBackCallback(activity));
Summary
Public methods
finishAndRemoveTaskCallback
static fun finishAndRemoveTaskCallback(activity: Activity): OnBackInvokedCallback
Get a callback to triggers Activity.finishAndRemoveTask()
on the associated Activity
. If the activity is the root activity of its task, the entire task will be removed from the recents task. The activity will be finished in all cases. The system will play the corresponding transition animation.
Parameters |
activity |
Activity: The associated Activity This value cannot be null . |
moveTaskToBackCallback
static fun moveTaskToBackCallback(activity: Activity): OnBackInvokedCallback
Get a callback to triggers Activity.moveTaskToBack(boolean)
on the associated Activity
, moving the task containing the activity to the background. The system will play the corresponding transition animation, regardless of whether the activity is the root activity of the task.
Parameters |
activity |
Activity: The associated Activity This value cannot be null . |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-03-13 UTC.
[null,null,["Last updated 2025-03-13 UTC."],[],[],null,["# SystemOnBackInvokedCallbacks\n\nAdded in [API level 36](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)\n\nSystemOnBackInvokedCallbacks\n============================\n\n*** ** * ** ***\n\nKotlin \\|[Java](/reference/android/window/SystemOnBackInvokedCallbacks \"View this page in Java\") \n\n```\nclass SystemOnBackInvokedCallbacks\n```\n\n|---|--------------------------------------------------|\n| [kotlin.Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html) ||\n| ↳ | [android.window.SystemOnBackInvokedCallbacks](#) |\n\nUtility class providing [OnBackInvokedCallback](/reference/kotlin/android/window/OnBackInvokedCallback)s to override the default behavior when system back is invoked. e.g. [Activity.finish](../app/Activity.html#finish())\n\nBy registering these callbacks with the [OnBackInvokedDispatcher](/reference/kotlin/android/window/OnBackInvokedDispatcher), the system can trigger specific behaviors and play corresponding ahead-of-time animations when the back gesture is invoked.\n\nFor example, to trigger the [Activity.moveTaskToBack](../app/Activity.html#moveTaskToBack(kotlin.Boolean)) behavior: \n\n```kotlin\nOnBackInvokedDispatcher dispatcher = activity.getOnBackInvokedDispatcher();\n dispatcher.registerOnBackInvokedCallback(\n OnBackInvokedDispatcher.PRIORITY_DEFAULT,\n SystemOnBackInvokedCallbacks.moveTaskToBackCallback(activity));\n \n```\n\nSummary\n-------\n\n| Public methods ||\n|----------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| static [OnBackInvokedCallback](/reference/kotlin/android/window/OnBackInvokedCallback) | [finishAndRemoveTaskCallback](#finishAndRemoveTaskCallback(android.app.Activity))`(`activity:` `[Activity](../app/Activity.html#)`)` Get a callback to triggers [Activity.finishAndRemoveTask()](../app/Activity.html#finishAndRemoveTask()) on the associated [Activity](../app/Activity.html#). |\n| static [OnBackInvokedCallback](/reference/kotlin/android/window/OnBackInvokedCallback) | [moveTaskToBackCallback](#moveTaskToBackCallback(android.app.Activity))`(`activity:` `[Activity](../app/Activity.html#)`)` Get a callback to triggers [Activity.moveTaskToBack(boolean)](../app/Activity.html#moveTaskToBack(kotlin.Boolean)) on the associated [Activity](../app/Activity.html#), moving the task containing the activity to the background. |\n\nPublic methods\n--------------\n\n### finishAndRemoveTaskCallback\n\nAdded in [API level 36](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nstatic fun finishAndRemoveTaskCallback(activity: Activity): OnBackInvokedCallback\n```\n\nGet a callback to triggers [Activity.finishAndRemoveTask()](../app/Activity.html#finishAndRemoveTask()) on the associated [Activity](../app/Activity.html#). If the activity is the root activity of its task, the entire task will be removed from the recents task. The activity will be finished in all cases. The system will play the corresponding transition animation.\n\n| Parameters ||\n|------------|------------------------------------------------------------------------------------------------------------------|\n| `activity` | [Activity](../app/Activity.html#): The associated [Activity](../app/Activity.html#) This value cannot be `null`. |\n\n| Return ||\n|---------------------------------------------------------------------------------|------------------------------|\n| [OnBackInvokedCallback](/reference/kotlin/android/window/OnBackInvokedCallback) | This value cannot be `null`. |\n\n**See Also**\n\n- [android.app.Activity#finishAndRemoveTask()](../app/Activity.html#finishAndRemoveTask()) \n\n### moveTaskToBackCallback\n\nAdded in [API level 36](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nstatic fun moveTaskToBackCallback(activity: Activity): OnBackInvokedCallback\n```\n\nGet a callback to triggers [Activity.moveTaskToBack(boolean)](../app/Activity.html#moveTaskToBack(kotlin.Boolean)) on the associated [Activity](../app/Activity.html#), moving the task containing the activity to the background. The system will play the corresponding transition animation, regardless of whether the activity is the root activity of the task.\n\n| Parameters ||\n|------------|------------------------------------------------------------------------------------------------------------------|\n| `activity` | [Activity](../app/Activity.html#): The associated [Activity](../app/Activity.html#) This value cannot be `null`. |\n\n| Return ||\n|---------------------------------------------------------------------------------|------------------------------|\n| [OnBackInvokedCallback](/reference/kotlin/android/window/OnBackInvokedCallback) | This value cannot be `null`. |\n\n**See Also**\n\n- [android.app.Activity#moveTaskToBack(boolean)](../app/Activity.html#moveTaskToBack(kotlin.Boolean))"]]