Why do we have to call super in Android sometimes?

后端 未结 6 1079
暗喜
暗喜 2020-11-29 21:05

Sometimes when I override methods, I get an exception the first time it\'s called like below:

05-31 21:32:04.266: E/AndroidRuntime(28471): android.support.v4         


        
6条回答
  •  再見小時候
    2020-11-29 21:35

    Why are we forced to call super.method()?

    The classes that make up the Android SDK can be incredibly complex. For instance, both activities and fragments must perform a number of operations in order to function properly (i.e. managing life cycle, optimizing memory usage, drawing the layout to the screen, etc.). Requiring the client to make a call to the base class (often at the beginning of the method) ensures that these operations are still performed, while still providing a reasonable level of abstraction for the developer.

    How do we know that a function method requires super to be called?

    The documentation should tell you whether or not this is required. If it doesn't I'd Google-search for some sample code (or check the API demos... or better yet, look at the source code!). It shouldn't be too difficult to figure out.

提交回复
热议问题