Android - Activity constructor

前端 未结 4 862
执念已碎
执念已碎 2020-12-20 09:06

I noticed that using the shortcut Alt + Insert and selecting the builders, it tries to create a constructor with each private property (e.g. cManager

4条回答
  •  心在旅途
    2020-12-20 09:18

    All of your initializations should be performed in the onCreate() method of your Activity:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.cManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
        this.mTextView = (TextView)this.findViewById(R.id.mProgressText);
    }
    

    Overriding the constructor of Activity involves quite a bit of heavy lifting, and is really not a walk in the park. Although you can [of course] have an empty constructor for an Activity, it really is quite superfluous in the context of the Android framework.

    Related answers:

    1. Why I cannot pass parameters to Android Activity Constructor.

    2. Start an Activity with a parameter.

提交回复
热议问题