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

JavaAndroid

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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

Solution 3 - Java

Unless you have multiple subclasses of Activity which 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).

Solution 4 - Java

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.

Solution 5 - Java

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

Solution 6 - Java

Calling super is mandatory now. It throws "Overriding method should call super.onActivityResult" error. Adding super won't hurt your code.

Solution 7 - Java

Well First you need to understand onActivityResult is a callback when there is an action done with intent like choosing, clicking a photo, etc, and super().onActivityResult() returns an exception when the activity of intent gets crashed or something like that happens. In short super().onActivityResult() is use to handle the error or exception during execution of Intent.

 protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    if (!mActivityResultRegistry.dispatchResult(requestCode, resultCode, data)) {
        super.onActivityResult(requestCode, resultCode, data);
    }

Exception

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    throw new RuntimeException("Stub!");
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPrizoffView Question on Stackoverflow
Solution 1 - JavahcarverView Answer on Stackoverflow
Solution 2 - JavaSu-Au HwangView Answer on Stackoverflow
Solution 3 - JavawsanvilleView Answer on Stackoverflow
Solution 4 - JavaSquonkView Answer on Stackoverflow
Solution 5 - JavaMikeView Answer on Stackoverflow
Solution 6 - JavaTowsif Ahamed LabibView Answer on Stackoverflow
Solution 7 - JavaKaranKulshresthaView Answer on Stackoverflow