On Dec 30 2007, 10:40 am, Rick Genter <rick_gen...@comcast.net
> This reminds me of a question I've been meaning to ask: When is the
> correct time to invoke the superclass's implementation of an on*()
Unfortunately, there isn't a simple rule: it entirely depends on the
method and what you want to do.
> So far I've been following the rule that the superclass's
> implementation should be invoked at the start of the overriding method
> when the method is in the "enabling" part of the lifecycle (onCreate,
> onStart(), onResume()) and at the end of the overriding method when
> the method is in the "disabling" part of the lifecycle (onPause(),
> onDestroy(), etc.).
For most of these hooks, I wouldn't worry too much about it -- the
onXxx() methods are there for the application developer to plug in
their own code, so the system classes generally take care of doing any
work that must happen before/after the application at the appropriate
point before/after the onXxx() method is called. This is certainly
the case for all of the Activity, IntentReceiver, Service, and
For the specific case of Activity.onCreate(), Activity.onResume(),
Activity.onPause(), Activity.onStop(), etc., you will be fine with
calling the super class implementation first. Of course, if you are
deriving from your own class, it may do work in one of these methods,
and you may need to call the super class in a particular order, but
that is up to you.
One thing that is slightly different is Activity.onKeyDown(), since
there is some default implementation supplied by the system (at least
causing BACK to finish the activity), so you may care more about when
you call the derived implementation. Generally for key events, I
would suggest first checking for any keys you are interested in
yourself, and then if it is not one of those calling through to the
super class and returning its result.
The view hierarchy classes are a little more complicated. For
example, standard widgets like TextView and ImageView implement
onDraw() to render themselves. If you are deriving from one of these,
you can decide when to the call the super implementation based on
where you want your own drawing to appear. There are also the
dispatchXxx() methods that you can override, and if so will very
carefully want to decide when (or even if) you call the super
implementation depending on the behavior you want to create.