Using Activity Result API in Xamarin.Android

515 Views Asked by At

It seems like it's not properly supported as I need to implement JNI stuff when I try to implement IActivityResultCallback interface. Also ActivityResultContract is not a generic class so when I inherit it to create a custom contract it's not type-safe like in native Android. So it's not usable at all at the moment I guess?

1

There are 1 best solutions below

0
On

You can get around this issue by inheriting from Java.Lang.Object. Something like this will probably work:

public class MyActivityResultCallback : Java.Lang.Object, IActivityResultCallback
{
}

IActivityResultCallback inherits from IJavaObject, which most, if not all Android interfaces do. This way Xamarin can interface between C# and Java. This is described pretty well here: https://learn.microsoft.com/en-us/xamarin/android/platform/java-integration/working-with-jni#implementing-interfaces