Search code examples
c#android.netmaui

OnBackPressedDispatcher.AddCallBack Not Available in .NET Android Maui


I am in the process of converting my Xamarin.Android app to .Net Android Maui due to the ended support for Xamarin. One of the functions involved in my app is overriding the hardware back button. In Xamarin.Android, I did this with no problem using OnBackPressedDispatcher.AddCallback & inheriting from OnBackPressedCallback. Inheriting from OnBackPressedCallback is no problem, but the AddCallback method seems to be missing from OnBackPressedDispatcher. What should I do to add my callback to the Activity? (NOTE: My app is Android only, so most of my code will be the same, I just need to replace the Xamarin-based code with the Maui-based equivalents). Any ideas? Thanks!


Solution

  • @Nathan Sokalsi,

    I'm sorry, but I gave you the wrong project name. The project that has the OnBackPressedCallback is called NavigationGraph7Net7. But as I said earlier, I no longer use it because it is no longer necessary. You won't find it in NavigationGraph8Net8 or any later projects. Could you explain why you think you need it? What are you trying to do with it? Also, what version of Android are you targeting, and what version of .Net?

    you also have PackageReferences using Xamarin, which is what we are supposed to be moving away from.

    I have already explained the above on the MS forum, but for some reason, several of your questions and their answers have been deleted from there. The last time I saw them was Friday evening (25/10), my time (AU), but now they are gone.

    You seem to be confused by the name Xamarin in the package names. They were never removed, and we aren't moving away from them with .Net. They are still just as pertinent as they ever were.

    You also seem to be confused by the inclusion of MAUI. If you are writing an Android-only app, you should refer to it as .NET for Android. I'd suggest removing the maui tag in your questions, as it confuses those offering help.

    It is only when you are doing cross-platform that MAUI comes into it. If you look on Discord/DotNetEvolution/#Android, you'll see a post 23/10/24 from jpobst, one of the MS Android team members, which should help clarify it for you.

    Xamarin.Android -> .NET for Android

    Xamarin.Forms -> MAUI

    Xamarin was already confusing because to some people it meant Xamarin.Android/Xamarin.iOS and to others, it meant Xamarin.Forms unfortunately we cannot change the NuGet package names as they will break the whole ecosystem, so they still say Xamarin even though they are .NET for Android/MAUI packages.

    Do you still need an answer regarding your now-deleted RecyclerView question?

    To specifically answer your question about OnBackPressCallback. You would need the NuGet Xamarin.AndroidX.Activity, which you probably already have either directly or transitively.

    Definition is: AndroidX.Activity.OnBackPressedDispatcher OnBackPressedDispatcher

    To use it, for instance, in a fragment

    onBackPressedCallback = new NavFragmentOnBackPressedCallback(this, true);
    RequireActivity().OnBackPressedDispatcher.AddCallback(ViewLifecycleOwner, onBackPressedCallback);