• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

How parameters are passed to event handler methods

Hi guys,

I use Java and Android Studio for developing apps. I use them since a while but I have still a doubt about the Android Events Mechanism and the way parameters required by the event handlers arrive to them. The event handlers are methods, more specifically callbacks, and can receive parameters. Usually, when you call a method that requires parameters you have to pass them to it but, in the case of event handlers, it seems that something obscure happens because the parameters are passed “automatically” to the callback. I tried to find explanations on Internet and books but with no results. For example, the View type parameter required by the onClick event handler, how is it passed? from where? I perfectly know that this parameter represents the object on which I have clicked but I want to know when and where the passing happens.
Another example: the onLocationChanged callback requires an input parameter of type Location... blah blah blah

Could you please explain in a clear and detailed way how the mechanism works? How this “automatic” parameter passage works? If I would to create a custom callback that works in this way, how could I accomplish this?

Thanks
F
 
What do you mean by 'create a custom callback'? That's actually a contradiction in terms, because any callback you want to use must conform to a published interface specification. Taking the Button click listener as an example - you must provide a class with the following method signature

Code:
View.OnClickListener() {
            public void onClick(View v) {
                
            }
        }

That means you must provide a class, containing exactly this method name, with exactly one parameter of type View, and register it with the Button object, via its setOnClickListener() method.

Now in terms of how this callback method is executed, you don't really need to know that in order to use it. If you're really interested in the code behind how this works, you can always view the Android framework code. For instance have a look at the setOnClickListener() method code. I presume there is something there which is registering your clickListener class. Whenever a click event happens for that Button, the framework then dispatches that event by invoking your onClick method.

But like I said, you really don't need to know exactly how all this works. Your only real concern is to implement the code within the onClick() method. That's where something gets done in your app's code, to handle that click.
 
What do you mean by 'create a custom callback'? That's actually a contradiction in terms, because any callback you want to use must conform to a published interface specification. Taking the Button click listener as an example - you must provide a class with the following method signature

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'><tt>&#160; </tt></li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>View.OnClickListener() { </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; public void onClick(View v) { </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; } </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; } </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>[/LIST] That means you must provide a class, containing exactly this method name, with exactly one parameter of type View, and register it with the Button object, via its setOnClickListener() method. </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>Now in terms of how this callback method is executed, you don't really need to know that in order to use it. If you're really interested in the code behind how this works, you can always view the Android framework code. For instance have a look at the setOnClickListener() method code. I presume there is something there which is registering your clickListener class. Whenever a click event happens for that Button, the framework then dispatches that event by invoking your onClick method. </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>But like I said, you really don't need to know exactly how all this works. Your only real concern is to implement the code within the onClick() method. That's where something gets done in your app's code, to handle that click.
</li>
</ul>
 
Back
Top Bottom