Why is onCreate() in Activity protected?

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

    the onCeate() is protected so to avoid calling of it from the activity object.

    MyActivity activity = new MyActivity();
    activity.onCreate(args);  // which doesn't make sense because activity is not yet created
    

    Since this method is only called when the activity gets created, calling it yourself will most probably give you a nullpointerException because the activity is not yet created.

提交回复
热议问题