java 是否需要在 onActivityResult() 中使用 super.onActivityResult()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11745366/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:12:54  来源:igfitidea点击:

Is there a need to use super.onActivityResult() in onActivityResult()?

javaandroid

提问by Prizoff

Which one is better and why?

哪个更好,为什么?

This one:

这个:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    ...
}

or this:

或这个:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    // do not call super.onActivityResult()
    ...
}

采纳答案by hcarver

The first one is better.

第一个更好。

It's more consistent with other event functions in the ActivityAPI, it costs you nothing (the code you're calling does nothing at the moment), and it means you don't need to remember to add the call in the future when the behaviour of the base class changes.

它与ActivityAPI中的其他事件函数更加一致,它不会花费您任何费用(您正在调用的代码目前什么都不做),这意味着您不需要记住在以后的行为发生时添加调用基类发生变化。

Edit

编辑

As Su-Au Hwang has pointed out, my prediction about the behaviour of the base class changing in the future has come true! FragmentActivityrequiresyou to call the method on super.

正如 Su-Au Hwang 所指出的,我对未来基类行为变化的预测已经实现了!FragmentActivity要求您在 上调用该方法super

回答by Su-Au Hwang

You should call super.onActivityResult if you are using FragmentActivity from the support package (also SherlockFragmentActivity). Otherwise it isn't necessary, however i'd just plug it in there for the sake of it. Check the source of FragmentActivity (no onActivityResult is not empty).

如果您正在使用支持包(也是 SherlockFragmentActivity)中的 FragmentActivity,您应该调用 super.onActivityResult。否则没有必要,但是我只是为了它而将它插入那里。检查 FragmentActivity 的来源(没有 onActivityResult 不为空)。

FragmentActivity source

片段活动源

回答by wsanville

Unless you have multiple subclasses of Activitywhich depend on it in your application, it doesn't look like calling super.onActivityResult()is needed, since the implementation of onActivityResult()is empty (I checked API level 15).

除非您Activity的应用程序中有多个依赖于它的子类,否则看起来super.onActivityResult()不需要调用,因为 的实现onActivityResult()是空的(我检查了 API 级别 15)。

回答by Squonk

You can answer this yourself by looking at the source code for Activity.

您可以通过查看Activity的源代码自己回答这个问题。

Basically it's implementation of onActivityResult(...)looks like this...

基本上它的实现onActivityResult(...)看起来像这样......

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}

...so does nothing.

......所以什么都不做。

回答by Mike

Although it seems the default implementation is empty, it's possible that in future updates that might not always be the case. I would recommend using it

尽管默认实现似乎是空的,但在未来的更新中可能并非总是如此。我会推荐使用它