javaandroidxamarin.androidandroid-runtime

How do you determine when interface methods are invoked at Runtime?


Consider the documentationfor the OnTouchListener which is clear:

Called when a touch event is dispatched to a view.

Perfect! I get it. I'm looking for this kind of description about interface methods which can be overloaded to create things like custom Animations, Rows, Adapters/whatever.

Take this documentation for the getView() method of the Adapter Interface as an example of my confusion. Its my understanding that getView() gets called by android anytime a new view is required by the adapter, like when you scroll through a list view and a new cell gets added. But I don't see how I could come to that conclusion from the documentation.

For getView() it seems intuitive. But other interfaces like SectionIndexor I don't think are. I frequently struggle overloading interface methods because I can't figure out how they interact at runtime. Is this documented?


Solution

  • The Microsoft documents define an interface as follows:

    An interface contains definitions for a group of related functionalities that a class or a struct can implement.

    There are minor differences in Java and C# interfaces which can be found here. (Assuming you to be from a Java background)

    Now you have a confusion in between abstract class's overridden method and an interface method. Check the difference here

    The GetView method is a method of the Android abstract class called BaseAdapter. Base Adapter abstract class and hence to get info on that first you need to look into BaseAdapter then find GetView method in it. And there you can get the exact description of the method and what it does. Note: that Xamarin.Android works exactly the same as native Android so you could use the same documentation for an understanding of the methods.

    Note: Implementation differs from C# to Java.

    Now an eg for an interface would be the IOnMapReadyCallback which is used as a callback by Xamarin.Android to check if the map is ready to be used.

    Now interfaces in C# as per their naming convention begin with an I. eg: the Android java OnTouchListener interface becomes the IOnTouchListener in Xamarin Android and so on and so forth.

    Now if you use an interface method this method is just defined and it is mandatory that you use that method in your class which you are inheriting it to, so this method will be added to that class and will not act as an overridden method like it does in case of abstract class.

    Now in case, you want to understand when an interface method is invoked you need to check the Android documentation for that interface eg the OnMapReadyCallback then find the method you need to understand i.e. onMapReady

    In case you don't understand anything revert.

    Goodluck!

    Happy coding.