简体   繁体   中英

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.

Edit

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

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

FragmentActivity source

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

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

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

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. It throws "Overriding method should call super.onActivityResult" error. Adding super won't hurt your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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