Xamarin.Android Implementing Java interfaces


Example

If a java library contains interfaces that should be implemented by the user (e.g. click listeners like View.IOnClickListener or callbacks), the implementing class has to inherit -- directly or indirectly -- from Java.Lang.Object or Java.Lang.Throwable. This is a common error, because the packaging steps just print a warning that is overlooked easily:

Type 'MyListener ' implements Android.Runtime.IJavaObject but does not inherit from Java.Lang.Object. It is not supported.

Wrong

The usage of this implementation will result in unexpected behavior.

class MyListener : View.IOnClickListener
{
    public IntPtr Handle { get; }

    public void Dispose()
    {            
    }

    
    public void OnClick(View v)
    {
        // ...
    }
}

Correct

class MyListener : 
    Java.Lang.Object, // this is the important part
    View.IOnClickListener
{       
    public void OnClick(View v)
    {
        // ...
    }
}