Locations of super() calls in Android Eclipse Plugin generated code reliable? Locations of super() calls in Android Eclipse Plugin generated code reliable? android android

Locations of super() calls in Android Eclipse Plugin generated code reliable?


This is a good question. Unfortunately, there is no simple rule for this. You need to know what the superclass implementation does. Sometimes (as in View.onDraw()), the superclass implementation does nothing; calling super() is both harmless and unnecessary. In other cases (such as Activity.onCreate()) the superclass implementation performs critical operations that must be executed at some point in the subclass's processing. Sometimes what happens when you call super() should come before any processing in the subclass, sometimes at other points. Sometimes you want to completely replace the superclass processing with your own, in which case you don't call super() at all. You have complete freedom to call the superclass version at any point (or even at multiple points) in your subclass's logic.

In constructors, the call to a superclass constructor (if present) must be the first thing in the method. If you don't have one, the compiler automatically inserts a call to the no-argument constructor in the superclass. (If the superclass does not have a no-argument constructor, or if it is not accessible to the subclass, the compiler generates an error.)

If the documentation doesn't provide enough information, then you have to look at the source code. The Android code is available here (Gingerbread release). The API code is under core.

EDIT The code is no longer available at git.kernel.org. Here are two other places where you can browse the code:

The main code is in the repository Platform > Frameworks > Base


Can I always rely on these code insertions locations in the generated code?

No, sometimes you don't want to call the super.method. Sometimes you want to call it first, sometimes at the last place, etc. It depends. But, I'm talking about methods, no constructors.

Is there a simple rule/explanation when to call super()?

You will always have to all super as the previous answer points. The only case where you don't call super is when the constructor of the super class has no parameters; in that case the compiler will put the super for you.

I do not understand exacly what the superclass does in each case so I always insert my code at the exact location of the //TODO tags

If you are in doubt (I'm talking about super methods), you can always take a look at the source code. Google Code Search is a good resource to do so. Then you can decide whether to put your code before or after the super method; or even, don't put the super method at all.

Keep in mind that not putting the super method is valid at compile time. But, some methods on android won't work unless you invoke the super method (for instance, the onResume method of the Activity class).

Also, sometimes you will decide whether to run the super method or not in runtime. Consider this classic example:

@Overridepublic boolean onKeyUp(int keyCode, KeyEvent event) {    if( KeyEvent.KEYCODE_BACK == event.getKeyCode() ){        return true;    }    return super.onKeyUp(keyCode, event);}

If the user pressed the back key, you won't call the super method. If the user didn't, you delegate the work to the super method.