开发者

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

开发者 https://www.devze.com 2023-02-25 05:30 出处:网络
I notice a problem when reading \"Hello, Android.\" When he implements the onCreate() in an Activity, it looks like:

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 and开发者_StackOverflow社区roid'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.

0

精彩评论

暂无评论...
验证码 换一张
取 消