Handling the Back Button in a .NET Android MAUI App

Nathan Sokalski 4,111 Reputation points
2024-10-22T02:22:17.16+00:00

I am in the process of converting my Xamarin.Android apps to .NET Android MAUI (NOTE: My MAUI app is Android only). In my Xamarin.Android apps, I used AndroidX.AppCompat.App.AppCompatActivity, but would like to use Android.App.Activity in the future (this is the one that Visual Studio 2022 used in the template, so I am assuming it is the one we are expected to use). Even though my app was already using the OnBackPressedCallback & related classes & methods, I can't seem to get it to work in my .NET Android MAUI app. I have read many posts about using OnBackPressedCallback & the deprecated OnBackPressed, but I can't seem to find any complete examples or tutorials. I want an example and/or tutorial that shows (not just mentions, but shows) everywhere that I need to add and/or modify code, as well as any usings that I may need. Can anybody help me?

Developer technologies .NET .NET MAUI
Developer technologies .NET Other
Developer technologies C#
{count} votes

2 answers

Sort by: Most helpful
  1. Graham McKechnie 441 Reputation points
    2024-10-23T04:20:03.63+00:00

    If you are writing a modern Android app for late-version APIs, why would you want to use Android.App.Activity? You were right the first time with AndroidX.AppCompat.App.AppCompatActivity.

    I don't know why the Android app template (either net8 or net9.0-android) defaults to Android.App.Activity because that is the first thing you would want to change it to AndroidX.AppCompat.App.AppCompatActivity.

    AndroidX is for modern Android development. Look at the dependencies in that template; there is not a single AndroidX Nuget.

    I suppose that template is just an example of MS trying to push people to Maui; either that, or they were just being lazy, or if you look at it another way, they are just introducing you to the .Net project structure.

    I keep pointing you to my GitHub page, where you will find examples of what you need to convert from Xamarin.Android. As you can see, you don't need to rush to .net.

    You may be better off keeping your Android app as Xamarin.Android. You are good (at least for another 12 months) with Android 14. It is only when you want to move to Android 15 that you have to convert to <TargetFramework>net9.0-android35</TargetFramework>

    Most of those projects have examples of OnBackPressedCallback. If you don't have an Android 15 device, remember to turn Predictive Back Animations on in Developer Options. You also need android:enableOnBackInvokedCallback="true" in the AndroidManifest.xml.

    The key points for predictive back. NavigationComponent, NavGraph, Single Activity and individual fragments with ConstraintLayouts for each screen.

    0 comments No comments

  2. Anonymous
    2024-10-24T08:33:01.5533333+00:00

    Hello,

    Based on your latest issue, you used .NET for Android project.

    If you want to use AndroidX.AppCompat.App.AppCompatActivity in .NET for Android project, you can add Xamarin.AndroidX.AppCompat nuget package in your project.

    Then, you need to Activity's theme to Theme = "@style/Theme.AppCompat".

    have read many posts about using OnBackPressedCallback & the deprecated OnBackPressed

    Yes, OnBackPressed is deprecated. You can use OnBackPressedDispatcher.AddCallback to do it and implement OnBackPressedCallback interface like following code. You can catch up back button pressed event in the HandleOnBackPressed method,

    [Activity(Label = "@string/app_name", Theme = "@style/MainTheme", MainLauncher = true)]
      public class MainActivity : AppCompatActivity
      {
          protected override void OnCreate(Bundle? savedInstanceState)
          {
              base.OnCreate(savedInstanceState);
     
              SetContentView(Resource.Layout.activity_main);
     
              setupBackPressedHandling();
     
     
          }
     
          private void setupBackPressedHandling()
          {
     
              OnBackPressedDispatcher.AddCallback(new MyBackPressedCallback(true));
          }
     
          
     
      }
     
      internal class MyBackPressedCallback : OnBackPressedCallback
      {
          private bool enabled;
          public MyBackPressedCallback(bool enabled) : base(enabled)
          {
              this.enabled = enabled;
          }
     
          public override void HandleOnBackPressed()
          {
              // throw new NotImplementedException();
              if (enabled) {
     
                  Console.WriteLine("========OnBackPressed get==============");
              }
          }
      }
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.