Why do the Activity API lifecycle methods use RuntimeException to force sub-classes to invoke super methods? -
For Android, it is necessary that all activity sub-classes call super methods from their lifecycle methods. The exception is thrown when super method is not used. Why does Android use a runtime exception mechanism to force super methods to say why it does not use the 'template' design pattern so that super methods can be executed automatically before hair methods. For example, Desty () can be handled in the following ways: -
Classroom activity {public void onDestroyFrmwork ()} / / Whatever Super Destroy () method Destroy (); // This will apply the subclass pattern. } Public at Zero Destroy () {// Empty Sub-classes will be overridden}}
I know that this question will be asked After 11 months I am answering this answer. I think the reason is that the order to call the super method can not be determined in advance. For example, I would like to clean myself before calling after super.onDestroy () , super.onDestroy () or even like this Can be mixed: @Override Safe void onDestroy (// {// some initial clean-up preDestroy (); // Then make a super-super call. // Finally some final cleaning up posteroy (); } This example is for argument; But I'm sure if you look quite tough then you can come to the real world examples.
This type of mixed order template design pattern will be difficult to use.
Comments
Post a Comment