java Android:覆盖 onPause 和 onResume - 正确的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12974630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: overriding onPause and onResume - proper way
提问by Genry
When overriding the onPause()
and the onResume()
methods of the activity, where is the proper location to call the super.onPause()
and super.onResume()
? At the beginning of the method or at the end?
当覆盖活动的onPause()
和onResume()
方法时,调用super.onPause()
和的正确位置在super.onResume()
哪里?在方法的开头还是结尾?
采纳答案by espinchi
From http://developer.android.com/guide/components/activities.html#ImplementingLifecycleCallbacks:
从http://developer.android.com/guide/components/activities.html#ImplementingLifecycleCallbacks:
Your implementation of these lifecycle methods must always call the superclass implementation before doing any work, as shown in the examples above.
这些生命周期方法的实现必须始终在执行任何工作之前调用超类实现,如上面的示例所示。
So, for lifecycle callbacks, like onPause()
and onResume()
, we should do super.onPause()
or super.onResume()
at the very beginning. For other methods, it all depends on the semantics of the super class.
所以,对于生命周期回调,比如onPause()
和onResume()
,我们应该在一开始就做super.onPause()
or super.onResume()
。对于其他方法,这一切都取决于超类的语义。
回答by Adam Arold
Update:This is the accepted answer and it contains a nice amount of good information, including a useful diagram, pulled together into one place. However, it appears to be incorrect, at least according to the current Android documentation which, as the poster points out, is the ultimate source for information on the SDK. Possibly the documentation was clarified after this answer was posted. But, in any case, don't stop reading with this answer, check out espinchi's answer below. It has the documentation on its side.
更新:这是公认的答案,它包含大量的好信息,包括一个有用的图表,汇总到一个地方。然而,它似乎是不正确的,至少根据当前的 Android 文档,正如发布者所指出的,它是 SDK 信息的最终来源。发布此答案后,可能会澄清文档。但是,无论如何,请不要停止阅读此答案,请查看下面的 espinchi 答案。它有文档。
Placement of the super
methods depends only on your preference. It would only matter if those methods were taking parameters OR if you were doing some concurrent work. For example if you do this:
super
方法的放置仅取决于您的偏好。只有这些方法是否接受参数或者您是否正在执行一些并发工作才重要。例如,如果你这样做:
@Override
protected void onPause() {
try {
someOtherThread.join();
} catch (InterruptedException e) {
LOG.e(e);
}
super.onPause();
}
it might block the thread and prevent super
from being called.
它可能会阻塞线程并阻止super
被调用。
I suggest that you should read all documentation available because they will help you much. For example this is what you can find in the onPause
javadoc. I bolded out the important parts:
我建议您应该阅读所有可用的文档,因为它们会对您有很大帮助。例如,这是您可以在onPause
javadoc 中找到的内容。我把重要的部分加粗:
Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed. The counterpart to onResume().
When activity B is launched in front of activity A, this callback will be invoked on A. B will not be created until A's onPause() returns, so be sure to not do anything lengthy here.
This callback is mostly used for saving any persistent state the activity is editing, to present a "edit in place" model to the user and making sure nothing is lost if there are not enough resources to start the new activity without first killing this one. This is also a good place to do things like stop animations and other things that consume a noticeable amount of CPUin order to make the switch to the next activity as fast as possible, or to close resources that are exclusive access such as the camera.
In situations where the system needs more memory it may kill paused processes to reclaim resources. Because of this, you should be sure that all of your state is saved by the time you return from this function. In general onSaveInstanceState(Bundle) is used to save per-instance state in the activity and this method is used to store global persistent data (in content providers, files, etc.)
After receiving this call you will usually receive a following call to onStop() (after the next activity has been resumed and displayed), however in some cases there will be a direct call back to onResume() without going through the stopped state.
Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.
当活动进入后台但尚未(尚未)被终止时,作为活动生命周期的一部分被调用。对应于 onResume()。
当活动 B 在活动 A 之前启动时,将在 A 上调用此回调。在 A 的 onPause() 返回之前不会创建 B,因此请确保不要在这里做任何冗长的事情。
此回调主要用于保存活动正在编辑的任何持久状态,向用户呈现“就地编辑”模型,并确保在没有足够资源启动新活动时不会丢失任何内容,而无需先终止此活动。这也是执行诸如停止动画和其他消耗大量 CPU以尽快切换到下一个活动或关闭独占访问的资源(例如相机)之类的操作的好地方。
在系统需要更多内存的情况下,它可能会杀死暂停的进程以回收资源。因此,您应该确保在您从此函数返回时保存了所有状态。通常 onSaveInstanceState(Bundle) 用于保存活动中的每个实例状态,该方法用于存储全局持久数据(在内容提供者、文件等中)
收到此调用后,您通常会收到对 onStop() 的后续调用(在恢复并显示下一个活动之后),但是在某些情况下,会直接调用 onResume() 而不经过停止状态。
派生类必须调用超类的此方法的实现。如果他们不这样做,将引发异常。
I do recommend this flowchart for you it will help your development tremendously:
我向你推荐这个流程图,它将极大地帮助你的发展:
回答by Edward Falk
It probably doesn't matter, but to know for sure, you need to know what the super methods are doing, and usually that information is not available to you.
这可能无关紧要,但要确定,您需要知道超级方法在做什么,而通常您无法获得这些信息。
My style is to call e.g. super.onCreate(), super.onResume(), etc. before the body of my own method, and e.g. super.onPause() and super.onDestroy() after the body of my own method.
我的风格是在我自己的方法体之前调用例如 super.onCreate()、super.onResume() 等,在我自己的方法体之后调用例如 super.onPause() 和 super.onDestroy()。
The theory behind this is that I like to let the super methods run first while building something up, just in case what I'm doing depends on what the superclass sets up first, and when tearing something down, I like to tear down my own stuff before the superclass tears down its stuff.
这背后的理论是,我喜欢在构建某些东西时让超级方法先运行,以防万一我在做什么取决于超类首先设置的内容,而在拆除某些东西时,我喜欢拆除我自己的在超类拆除它的东西之前的东西。
回答by Givi
There's no right or wrong.
That depends on what you do on your implementation of these methods.
Sometimes you'll want the super
to be before your code, and sometime after.
没有对与错。这取决于您对这些方法的实现做了什么。有时你会希望super
在你的代码之前,有时在之后。
回答by Rajesh Rajaram
回答by Deepak Bala
Delving into the android code, you can find that the framework sets a flag called mcalled when you call super.onPause(). This flag is later checked on resume by the framework.
深入android代码,可以发现框架在调用super.onPause()的时候设置了一个叫做mcall的标志。此标志稍后由框架在恢复时检查。
if (!mCalled) {
throw new SuperNotCalledException(
"Activity " + mComponent.toShortString() +
" did not call through to super.onResume()");
}
All you need to do is make sure the call is made to super and you are good. No other precaution is necessary.
您需要做的就是确保呼叫 super 并且您很好。不需要其他预防措施。