Why is onCreate() in Activity protected?

后端 未结 4 1153
旧巷少年郎
旧巷少年郎 2021-01-02 06:05

Why is the onCreate() in Activity protected?

or I should ask: why does it work?

Protected method can only be called in the inside of the class itself or it\'

4条回答
  •  攒了一身酷
    2021-01-02 06:52

    It's useful to have public onClick methods because you can "force" certain buttons to be clicked programmatically. A common example of this is causing the same to code to execute when the user presses the enter key, or pressed the Submit button.

    I don't think Android calls Activity.onCreate directly. Note that Activity inherits from Context (which does have a public constructor). It is my understanding that the constructor triggers some events to occur, and the onCreate/Pause/Resume/Destroy methods are called internally to the class at the appropriate time.

    For example, when you create an activity, the view XML file has to be parsed and inflated. This happens automatically, so there's something happening behind the scenes that you don't directly control.

提交回复
热议问题