简体   繁体   English

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

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

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()
    ...
}

The first one is better.第一个更好。

It's more consistent with other event functions in the Activity API, 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.它与Activity API中的其他事件函数更加一致,它不花费你任何费用(你正在调用的代码现在什么都不做),这意味着你不需要记住在未来的行为时添加调用基类的变化。

Edit编辑

As Su-Au Hwang has pointed out, my prediction about the behaviour of the base class changing in the future has come true!正如 Su-Au Hwang 所指出的,我对未来基类行为变化的预测已经实现了! FragmentActivity requires you to call the method on super . FragmentActivity要求您在super上调用该方法。

You should call super.onActivityResult if you are using FragmentActivity from the support package (also SherlockFragmentActivity).如果您正在使用支持包(也是 SherlockFragmentActivity)中的 FragmentActivity,您应该调用 super.onActivityResult。 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).检查 FragmentActivity 的来源(没有 onActivityResult 不为空)。

FragmentActivity source 片段活动源

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

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. ......所以什么都不做。

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我会推荐使用它

Calling super is mandatory now.现在调用 super 是强制性的。 It throws "Overriding method should call super.onActivityResult" error.它抛出“覆盖方法应该调用 super.onActivityResult”错误。 Adding super won't hurt your code.添加 super 不会损害您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM