Why does a protected android:onClick method in Activity actually work?

tynn :

Suppose you define android:onClick="doClick" in your Activity as

protected void doClick(View view) { }

The documentation states that

This name must correspond to a public method that takes exactly one parameter of type View.

This is a given requirement of the underlying Class.getMethod() method, which only finds public methods as the documentation states that it

Returns a Method object that reflects the specified public member method of the class or interface represented by this Class object.

So how is it possible, that this implementation, which should not work at all, works on some devices and emulators, while it doesn't work on others using the same API levels as well?

tynn :

I debugged the particular implementations. The relevant portion of the code is within the Support Library using Class.getMethod().

As stated in the documentation, this method only finds public member methods and behaves correctly. For some reason, all modifiers of declared protected methods of the Activity (these are onCreate() and doClick()) are set to 1, which means these are actually public.

I could only observe this behavior creating the debug build with a Mac. So why this happens is still an open question, which I'm trying to find an answer to.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=34994&siteId=1