Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should the call to the superclass method be the first statement?

Tags:

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);
}
like image 297
enzom83 Avatar asked Mar 08 '12 22:03

enzom83


People also ask

Why call super must be first in constructor?

The Eclipse compiler says, Constructor call must be the first statement in a constructor . So, it is not stopping you from executing logic before the call to super() . It is just stopping you from executing logic that you can't fit into a single expression.

How do you call a super class method?

To call methods of the superclass that is overridden in the subclass. To access attributes (fields) of the superclass if both superclass and subclass have attributes with the same name. To explicitly call superclass no-arg (default) or parameterized constructor from the subclass constructor.

How do you call the super class method in subclass?

Subclass methods can call superclass methods if both methods have the same name. From the subclass, reference the method name and superclass name with the @ symbol.

What are the conditions for using super () method?

We can use super keyword to access the data member or field of parent class. It is used if parent class and child class have same fields. In the above example, Animal and Dog both classes have a common property color. If we print color property, it will print the color of current class by default.


2 Answers

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.

like image 152
CommonsWare Avatar answered Sep 27 '22 22:09

CommonsWare


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

like image 22
dcow Avatar answered Sep 27 '22 23:09

dcow