Dialog.OnBackPressed Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
This method was deprecated in API level 33.
[Android.Runtime.Register("onBackPressed", "()V", "GetOnBackPressedHandler")]
public virtual void OnBackPressed();
[<Android.Runtime.Register("onBackPressed", "()V", "GetOnBackPressedHandler")>]
abstract member OnBackPressed : unit -> unit
override this.OnBackPressed : unit -> unit
- Attributes
Remarks
This method was deprecated in API level 33. Use OnBackInvokedCallback or androidx.activity.OnBackPressedCallback to handle back navigation instead. Starting from Android 13 (API level 33), back event handling is moving to an ahead-of-time model and onBackPressed() and KeyEvent.KEYCODE_BACK should not be used to handle back events (back gesture or back button click). Instead, an OnBackInvokedCallback should be registered using Dialog.getOnBackInvokedDispatcher() .registerOnBackInvokedCallback(priority, callback).
Called when the dialog has detected the user's press of the back key. The default implementation simply cancels the dialog (only if it is cancelable), but you can override this to do whatever you want. If you target version Build.VERSION_CODES.TIRAMISU or later, you should not use this method but register an OnBackInvokedCallback on an OnBackInvokedDispatcher that you can retrieve using getOnBackInvokedDispatcher(). You should also set android:enableOnBackInvokedCallback="true" in the application manifest. Alternatively, you can use androidx.activity.ComponentDialog.getOnBackPressedDispatcher() for backward compatibility.
Android reference for android.app.Dialog.onBackPressed.
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.