20

The results of a speech recognition can be read in the onActivityResult(int requestCode, int resultCode, Intent data) method, as shown in this example. This method overrides the same method in class Activity: why is the call to the superclass method not the first statement?

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
        // Fill the list view with the strings the recognizer thought it could have heard
        // ...
    }

    super.onActivityResult(requestCode, resultCode, data);
}
Jonik
  • 80,077
  • 70
  • 264
  • 372
enzom83
  • 8,080
  • 10
  • 68
  • 114

2 Answers2

86

Methods you override that are part of component creation (onCreate(), onStart(), onResume(), etc.), you should chain to the superclass as the first statement, to ensure that Android has its chance to do its work before you attempt to do something that relies upon that work having been done.

Methods you override that are part of component destruction (onPause(), onStop(), onDestroy(), etc.), you should do your work first and chain to the superclass as the last thing. That way, in case Android cleans up something that your work depends upon, you will have done your work first.

Methods that return something other than void (onCreateOptionsMenu(), etc.), sometimes you chain to the superclass in the return statement, assuming that you are not specifically doing something that needs to force a particular return value.

Everything else -- such as onActivityResult() -- is up to you, on the whole. I tend to chain to the superclass as the first thing, but unless you are running into problems, chaining later should be fine.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 12
    But why official doc says: "Always call the superclass method first" in onPause()? http://developer.android.com/training/basics/activity-lifecycle/pausing.html#Pause – petrnohejl Aug 16 '12 at 14:25
  • 1
    This is an important answer. I wish it were in the official docs. – likejudo May 17 '14 at 02:40
  • Not necessarily true. Quote: "It is crucial to call `AndroidInjection.inject()` before `super.onCreate()` in an `Activity`, since the call to `super` attaches `Fragments` from the previous activity instance during configuration change, which in turn injects the `Fragment`s. In order for the `Fragment` injection to succeed, the `Activity` must already be injected." Same goes for ordinary Dagger injection. – arekolek May 17 '18 at 10:35
  • 1
    @arekolek: In 2012, when this answer was written, none of that existed. Now, in 2018, I agree that some specific libraries or other things might require you to take action before the `super.onCreate()` call. In general (i.e., if you are not using such libraries), I stand by this answer. – CommonsWare May 17 '18 at 10:38
  • 1
    There is a `call the superclass method first` comment in `onStop()` at [docs](https://developer.android.com/guide/components/activities/activity-lifecycle#onstop) also. – Fredrick Gauss May 31 '18 at 14:03
4

Because you generally want to perform the events unique to your overridden activity before passing the control back up the class hierarchy. Note that it is not always the case. Sometimes you should put the calls first such as in the callbacks that happen when your app is initialized, and you might want to put them last for events that happen when your app is destroyed so that you can clean up first.

In general though it doesn't matter and if it does it will be mentioned in the SDK -- I've ran into it mentioned a few places in the SKD (I think on documentation regarding dialogs) but I can't remember exactly which page/section it's on.

There is some more detailed discussion on the topic here: http://groups.google.com/group/android-developers/browse_thread/thread/9ddb2b06c21c8457

dcow
  • 7,765
  • 3
  • 45
  • 65