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

前端 未结 2 1850
广开言路
广开言路 2021-02-01 15:51

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

protected void doClick(View view) { }

The document

2条回答
  •  失恋的感觉
    2021-02-01 16:15

    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.

提交回复
热议问题