它已被弃用,因为他们现在希望您使用Java8并实现接口DefaultLifecycleWatch.由于Java8允许接口具有默认实现,因此它们使用所有方法的空实现定义了DefaultLifecycleWatch,因此您只需要覆盖您使用的方法.
The old way of marking functions with @OnLifecycleEvent
was a crutch for pre-Java 8 projects. This was the only way to allow a class to selectively choose which lifecycle events it cared about. The alternative would have been to force those classes to override all the lifecycle interface methods, even if leaving them empty.
在您的示例中,将类更改为实现DefaultLifecycleWatch,并更改函数以覆盖DefaultLifecycleWatch的适用函数.如果您的项目尚未使用Java8,则需要更新Gradle构建文件.把这些放到你模块build.gradle
的android
个挡路里:
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}