简体   繁体   中英

Where should I put the super call in Android event handler?

I notice a problem when reading "Hello, Android."

When he implements the onCreate() in an Activity, it looks like:

{
super.onCreate(..);
...
...
}

But onSizeChanged() looks like:

{
...
...
super.onSizeChange();
}

And he doesn't call super in onDraw().

Where should I put the super call statement? And where can I find answer in android's document?

In any overridden method in a sub class, the location of super.methodname() is usually the first thing in that method, (like in onCreate() ). However, sometimes you need to do additional steps before calling super, so you do this as in onSizeChanged() method. The super call is determined by this criteria, not by any other rule.

In a lot of cases, a useful answer to this question comes when you think about where your overridden functionality occurs in the grand scheme of things.

If you're calling something that behaves as an initializer, then the super call should (usuall) come at the beginning, because you want the base class to be initialized before you try to initialize your sub class.

If you're calling something that behaves as a clean-up routine, then the super call should (usually) come at the end, because you want to clean up all of your pieces before the super classes clean themselves up.

For calls that come at arbitrary times, it will depend on the nature of your overridden call. If you want to do something before the framework does its computations, so that you can influence their outcome, you will want the super call at the end. If you want to base your calculations on the computations of the super class, your super call should become at the beginning.

In short, there's no fixed rule for this: it depends on the functionality of the particular method you're overriding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM