为什么 java Android 中的 super.onDestroy() 在析构函数中排在首位?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4423671/
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
Why does super.onDestroy() in java Android goes on top in destructors?
提问by Vassilis
According to which logic does super.onDestroy();
in destructors goes on top? For example:
根据哪个逻辑super.onDestroy();
在析构函数中排在最前面?例如:
protected void onDestroy() {
super.onDestroy();
releaseMediaPlayer();
}
and not:
并不是:
protected void onDestroy() {
releaseMediaPlayer();
super.onDestroy();
}
Like in c++, obj-c, pascal, etc?
就像在 c++、obj-c、pascal 等中一样?
采纳答案by Cristian
It really depends on what you want to do in your onDestroy
. This is what super.onDestroy does (in that order):
这真的取决于你想在你的onDestroy
. 这就是 super.onDestroy 所做的(按此顺序):
- Dismiss any dialogs the activity was managing.
- Close any cursors the activity was managing.
- Close any open search dialog
- 关闭活动正在管理的任何对话框。
- 关闭活动正在管理的所有游标。
- 关闭任何打开的搜索对话框
If the logic you put inside onDestroy
has something to do with those three things that android does, then you may have to worry about the order. Otherwise, and in most of the cases, it does not matter.
如果你放在里面的逻辑onDestroy
和android做的那三件事有关系,那么你可能要担心顺序了。否则,在大多数情况下,这无关紧要。
回答by Falmarri
What's your question? You can do it either way, it depends if you want your superclass's onDestroy()
called before yours. Usually I don't think it matters in android.
你有什么问题?您可以采用任何一种方式,这取决于您是否希望在您的超类onDestroy()
之前调用超类。通常我认为这在android中并不重要。
Also,
onDestroy()
isn't a destructor. It doesn't actually destroy the object. It's just a method that's called based on a certain state. So your instance is still alive and very well* after the superclass's onDestroy()
runs and returns.
此外,
onDestroy()
不是析构函数。它实际上并没有破坏对象。它只是一种基于特定状态调用的方法。因此,在超类onDestroy()
运行和返回之后,您的实例仍然存在并且运行良好* 。
*Most likely, android is free to kill the activity at any time, but you can assume it's still there.
*最有可能的是,android 可以随时终止 Activity,但您可以假设它仍然存在。
回答by Zorfling
In the ThreadSample.zip on the Reporting Work Status training, there is a comment in onDestroy()
在报告工作状态培训的 ThreadSample.zip中,onDestroy() 中有一个注释
public void onDestroy() {
...
// Must always call the super method at the end.
super.onDestroy();
}
So perhaps when using Broadcast Receivers, the super must go at the end.
所以也许在使用广播接收器时,超级必须放在最后。
回答by siva
Since we are extending from the base android classes, it is always good approach to let the parent class create and initialize itself first during the creation and let the child uninitialize and free the resource first during shutdown/stopping the components. This is the recommended approach to be followed. however, it entirely depends on the use cases and scenarios.
由于我们是从基础 android 类扩展而来的,在创建过程中让父类首先创建和初始化自己,让子类在关闭/停止组件期间首先取消初始化并释放资源,这总是一个好方法。这是建议遵循的方法。但是,这完全取决于用例和场景。
public void onCreate(Bundle bundle){
super.onCreate(bundle);
//perform child class initializations.
}
public void onDestroy(){
//perform uninitialization and free resource
super.onDestroy();
}