Monday, September 23, 2013

How to Implement "IntPtr Handle and Dispose" in Xamarin.Android

Now you might have come by some interfaces you want to implement and then all of the sudden they want you to implement a Handle property which is of the type IntPtr and a Dispose method. If you leave them be your code will most likely never get called. Then you scratch your head and wonder why this is, also you might ask yourself what this Handle is. The brief explanation is that the Handle provides an Android Callable Wrapper to Android. All native Android interfaces extend IJavaObject so they will expect that to be implemented. So the simple answer is that you do not implement Handle or Dispose yourself, however you inherit from Java.Lang.Object which does that for you. So your code will end up looking like this:


Given that IStuff extends IJavaObject. More on this topic can be read in the Xamarin documentation on Android Callable Wrappers