Xamarin C# alternative to OnBackPressed which has been deprecated

fbs419 46 Reputation points
2022-10-13T14:07:18.77+00:00

I need some sample code on how to migrate from OnBackPressed, as I now need to target API 33. I've seen posts like:

https://medium.com/tech-takeaways/how-to-migrate-the-deprecated-onbackpressed-function-e66bb29fa2fd

which is java. For C#, I've seen this:

https://vladislavantonyuk.azurewebsites.net/articles/Migrate-the-deprecated-OnBackPressed-function-in-.NET-MAUI-Android-application

but couldn't get that to work. My OnBackPressed starts another activity, and "this" never worked right. I've also seen Kotlin samples, but nothing really for Xamarin.

I use Visual Studio 2022 and tried installing NuGet AndroidX packages, compatibility packages, and it was just a rathole. I even tried migrating the product to AndroidX, and that was a pain as well.

What is the best way to migrate this simple OnBackPressed code:

	public override void OnBackPressed ()  
	{  
		var myActivity= new Intent (this, typeof(MyActivity));  
		StartActivity (myActivity);  
	}  

I've tried the stuff in these posts but couldn't get it working. Any sample Xamarin code out there? Thanks a lot.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,305 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Michael Taylor 49,251 Reputation points
    2022-10-13T14:53:45.467+00:00

    You should use the methods that the docs recommend that you use. If you cannot get them to work then please post example code of what is not working with the newer methods and tell us exactly what errors you're getting. We have no way of knowing why your code isn't working with the new stuff without seeing it fail.


  2. Graham McKechnie 321 Reputation points
    2022-10-22T04:29:56.717+00:00

    @fbs419

    If you are going to target Android 13 it is not optional to use androidx packages - you have to. The androidx packages were introduced in Android 10, so they aren't exactly new, so you will have to jump into your rat hole. The migration idea was meant to be an assistance package in helping you migrate, but when I tried it years ago, it really wasn't all that useful. It is far easier just to replace all the old Support packages with their equivalent AndroidX packages. In fact, I don't think the migration package is even supported anymore.

    I have a number of tutorials using Android's NavigationComponent (Google's recommendation for modern Android development - Single Activity/Multiple Fragments) that would help/guide you in converting an existing old project.

    An OnBackPressedCallback class (C# version of the Kotlin code above) is introduced in the second project NavigationGraph2 to handle back key and back gestures and is used in all the following projects. You'll see in each fragment of each of the projects how the OnBackPressedCallback class is instantiated.

    The medium article above regarding predictive back gesture is a step further (only available via Settings developer options) that is at the moment not even available to Xamarin developers until Xamarin.Android.AppCompat 1.6.0 is released - the latest version is 1.5.1, so there is no immediate need to concern yourself with that feature.

    The .dot Maui Android example, var Navigation I think uses the NavigationComponent internally, however, I don't understand the use of the following code

    activity.FinishAndRemoveTask();
    Process.KillProcess(Process.MyPid());

    as that would kill the application and so prevent a warm start. You would typically use the above code if you were testing the SplashScreen API to ensure that each time you started the app, it would always do a cold start so that you could confirm that the application icon displays correctly.

    Your idea of starting an Activity via a back key press or back gesture just doesn't fit with modern Android development, so you will need to come up with a more appropriate method such as a menu item etc. Back key and back gestures are for navigating backwards - not forwards.

    You can find these tutorials at https://github.com/gmck. Each project has the same docx file, so you can read ahead before you tackle each project.

    0 comments No comments

  3. Graham McKechnie 321 Reputation points
    2023-03-09T04:08:18.0666667+00:00

    This answer expands on my previous answer.

    This test app demonstrates how to replace the now deprecated Activity.OnBackPressed with an OnBackPressedCallback in conjunction with using the new Predictive Back Gesture for Android 13

    It, first determines if the user is using Gesture navigation.

    See https://github.com/gmck/PredictiveBackGestureNet7

    0 comments No comments