How do I force a polymorphic call to the super method?

前端 未结 7 1038
猫巷女王i
猫巷女王i 2020-12-16 15:54

I have an init method that is used and overridden through out an extensive heirarchy. Each init call however extends on the work that the previous did. So naturally, I would

7条回答
  •  南方客
    南方客 (楼主)
    2020-12-16 16:13

    Nowadays you can annotate your method with @CallSuper. This will Lint check that any overrides to that method calls super(). Here's an example:

    @CallSuper
    protected void onAfterAttached(Activity activity) {
        if (activity instanceof ActivityMain) {
            mainActivity = (ActivityMain) activity;
        }
    }
    

    In the example above, any methods in descendant classes that override onAfterAttached but do not call super will make Lint raise an error.

提交回复
热议问题