Should the call to the superclass method be the first statement?

余生颓废 提交于 2019-11-27 10:49:11

Methods you override that are part of component creation (onCreate(), onStart(), onResume(), etc.), you should chain to the superclass as the first statement, to ensure that Android has its chance to do its work before you attempt to do something that relies upon that work having been done.

Methods you override that are part of component destruction (onPause(), onStop(), onDestroy(), etc.), you should do your work first and chain to the superclass as the last thing. That way, in case Android cleans up something that your work depends upon, you will have done your work first.

Methods that return something other than void (onCreateOptionsMenu(), etc.), sometimes you chain to the superclass in the return statement, assuming that you are not specifically doing something that needs to force a particular return value.

Everything else -- such as onActivityResult() -- is up to you, on the whole. I tend to chain to the superclass as the first thing, but unless you are running into problems, chaining later should be fine.

Because you generally want to perform the events unique to your overridden activity before passing the control back up the class hierarchy. Note that it is not always the case. Sometimes you should put the calls first such as in the callbacks that happen when your app is initialized, and you might want to put them last for events that happen when your app is destroyed so that you can clean up first.

In general though it doesn't matter and if it does it will be mentioned in the SDK -- I've ran into it mentioned a few places in the SKD (I think on documentation regarding dialogs) but I can't remember exactly which page/section it's on.

There is some more detailed discussion on the topic here: http://groups.google.com/group/android-developers/browse_thread/thread/9ddb2b06c21c8457

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