Android的实施生命周期方法可以调用做任何工作后,超类实现? [英] Android implementation of lifecycle methods can call the superclass implementation after doing any work?

查看:198
本文介绍了Android的实施生命周期方法可以调用做任何工作后,超类实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android的文件有:

注意:您执行这些生命周期方法做任何工作之前,必须始终调用父类的实现......
http://developer.android.com/guide/components/activities.html#ImplementingLifecycleCallbacks

但我所看到其中code为放置在超类方法后,特别是对于像的onPause(),的onStop(),的onDestroy()的方法,例如例:

In the Android documentation we have:

Note: Your implementation of these lifecycle methods must always call the superclass implementation before doing any work...
http://developer.android.com/guide/components/activities.html#ImplementingLifecycleCallbacks

But I have seen cases where the code is placed after the superclass method, especially for methods like onPause(), onStop(), onDestroy(), for example:

@Override
protected void onPause() {
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
    super.onPause();
} 

<一个href="http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html#ownreceiver_localbroadcastmanager" rel="nofollow">http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html#ownreceiver_localbroadcastmanager

http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html#ownreceiver_localbroadcastmanager

在这两个方面,它的工作原理。那么,什么是把code 0之前调用父类方法后有什么区别?什么是正确的方法是什么?

In both ways it works. So, what's the difference between putting the code before o after calling the superclass method? What's the correct way?

推荐答案

从CommonsWare的这个答案的直接复制。因此,最好给他upvote

Directly copied from CommonsWare's this answer. So better give him the upvote

方法,你会覆盖组件创建的一部分   (的onCreate() ONSTART() onResume()等),你应该链   超作为第一个发言,以确保Android有其   你尝试做一些依赖之前有机会完成其工作   在已经完成的工作。

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.

方法,你会覆盖成分破坏的一部分   (的onPause()的onStop()的onDestroy()等),你应该做的   第一,连锁合作,在超类的最后一件事。这样一来,在   Android的情况下清理东西,你的工作取决于,你会   已经首先做你的工作。

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.

这是返回的东西以外无效方法   ( onCreateOptionsMenu()等),有时你连锁超   在return语句,假设你是不是专门做   一些需要强制一个特定的返回值。

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.

其他的一切 - 如 onActivityResult() - 是你的,就   全部。我倾向于链的超类的第一件事情,但   除非你遇到了问题,后来链应该就可以了。

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.

但是,如果没有扶养,那么你想要的任何地方调用父类的方法。

But if there is no dependancy, then call the superclass methods anywhere you want.

这篇关于Android的实施生命周期方法可以调用做任何工作后,超类实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆