Android - which Activity methods require a super constructor?

风格不统一 提交于 2019-12-11 19:05:12

问题


In most Android apps, onCreate() is overridden with the first bit of code being super.onCreate(savedInstanceState) and I know this gathers the savedInstanceState Bundle and is neccessary for compilation but what about all of the constructors?

onResume(), onStop(), onStop() etc. Which overridden methods have important super constructors you need to include?

Is there a list somewhere?

I see the super constructors included in some code, not in others.. I have included them sometimes myself, others not and have never really noticed a difference..

Any light that could be shed would be well received!

Cheers


回答1:


onCreate(), onStart() and onResume() are used to startup the activity while on onStop() and onDestroy() are used to stop or clean up the activity.

As per the documentation you need to call the super for each of the method.

Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.

For more info




回答2:


Check this documentation.

The entire lifecycle of an activity is defined by the following Activity methods. All of these are hooks that you can override to do appropriate work when the activity changes state. All activities will implement onCreate(Bundle) to do their initial setup; many will also implement onPause() to commit changes to data and otherwise prepare to stop interacting with the user. You should always call up to your superclass when implementing these methods.

public class Activity extends ApplicationContext {
    protected void onCreate(Bundle savedInstanceState);

    protected void onStart();

    protected void onRestart();

    protected void onResume();

    protected void onPause();

    protected void onStop();

    protected void onDestroy();
}



回答3:


If you read the source code, you'll notice how in Activity.java those 6 methods have code inside, so I'd say you should call super for every overriden onXXX method.



来源:https://stackoverflow.com/questions/14603598/android-which-activity-methods-require-a-super-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!