Why is onCreate() in Activity protected?

后端 未结 4 1158
旧巷少年郎
旧巷少年郎 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:46

    onCreate is not public because you don't want a random class in a totally different package creating an activity.

    If I design an Activity class in a com.abccompany.activity package, and one of my co-workers designs a JSON data parsing class in a com.abccompany.jsonparser package, I don't want him creating my activity and displaying his JSON data whenever he wants.

    onCreate is not private because you do want to subclass an Activity and then use the super Activity onCreate method for the subclass.

    Actually every Activity you design extends android.app.Activity, so if onCreate was private in that super class, then you wouldn't be able to call onCreate at all.

提交回复
热议问题