Dialog.OnBackPressed Method

Definition

Called when the dialog has detected the user's press of the back key.

[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

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 android.os.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.

This member is deprecated. Use OnBackInvokedCallback or androidx.activity.OnBackPressedCallback to handle back navigation instead. <p> 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()OnBackInvokedDispatcher#registerOnBackInvokedCallback(int, OnBackInvokedCallback) .registerOnBackInvokedCallback(priority, callback).

Java documentation 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.

Applies to