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

Viewed 20388

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

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

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!");
}
Related