Should the call to the superclass method be the first statement?
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);
}
Solution 1:
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.
Solution 2:
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