[FIXED] Xamarin.Android predictive back gesture (OnBackPressed deprecated)


In Android 13 the OnBackPressed method has become deprecated. How can i implement this code in Xamarin.Android?

void onCreate() {
  if (BuildCompat.isAtLeastT()) {
        () -> {
          * Code here

The code above is the Java example from the official android dev website, but i can’t find the replacements for this in Xamarin.
Can someone translate this to C#?

This is what Visual studio suggests, but it doesn’t help at all.

Warning CS0672:Member 'MainActivity.OnBackPressed()' overrides obsolete member 'Activity.OnBackPressed()'. Add the Obsolete attribute to 'MainActivity.OnBackPressed()'.    


You can try to use OnBackPressedDispatcher.AddCallback()


    protected override void OnCreate(Bundle savedInstanceState)
        if (BuildCompat.IsAtLeastT)
        OnBackPressedDispatcher.AddCallback(new CustomOnBackPressed(true));


public class CustomOnBackPressed : OnBackPressedCallback
    public OnBack(bool enabled) : base(enabled)

    public override void HandleOnBackPressed()
        //code that handles back button pressed

Related question

onBackPressed() deprecated, What is the alternative?

Answered By – Cfun

Answer Checked By – Robin (FixeMe Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *