'Android: Custom annotation the requires calling of a method depending on the set value of the annotated variable

Something that is similar to @RequiresPermission("android.permission.INTERNET") that will ask for permission when a certain annotated method is used.

Can you create a custom annotation that will require the user to call a method after setting a value of the annotated variable?

Example

protected abstract var interceptBackPress: Boolean

I want to require or warn the consumer (child class) that when interceptBackPress is set to false, they will need to call a method requireActivity().onBackPressed(). While when set to true they will be required to override a method from the parent class.

Example method to override

    @EmptySuper
    open fun onBackPressed() {
    }

Note that both interceptBackPress and onBackPressed() is define in the parent class.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source