LifecycleOwner.getLifecycle多次调用

时间:2018-02-21 13:17:27

标签: android kotlin dagger-2 android-lifecycle

出于某种原因,我无法理解为什么当我将应用程序放在后台时,我的类project.vasile.emanuel.gresanu.cemdmobilediagnosis2.ui.base.BaseFragment中的方法“getLifecycle()”被多次调用。更确切地说,按原样运行代码(我已经为您进行了测试的特殊情况)并将应用程序放在后台。从这里开始,logcat被归档为“getLifecycle()”被调用为无限的消息。该应用的链接:https://drive.google.com/file/d/1xZ8unSEwgkumPRUj2BpwsL9hKk1XJLrU/view?usp=sharing

请帮忙

相关的类是project.vasile.emanuel.gresanu.cemdmobilediagnosis2.ui.base.BaseFragment,因为我正在实现LifecycleOwner,并覆盖方法getLifecycle()。

代码:

abstract class BaseFragment: dagger.android.support.DaggerFragment(), LifecycleOwner {
    var lifeCycleRegistry: LifecycleRegistry

    init {
        AppLogger.d("BaseFragment.init")
        this.lifeCycleRegistry = LifecycleRegistry(this@BaseFragment)
    }
    //....
    override fun getLifecycle(): Lifecycle {
        AppLogger.d("BaseFragment.getLifecycle and count: ${this.lifeCycleRegistry.observerCount}, ${this.lifeCycleRegistry.currentState}")
        return this.lifeCycleRegistry
    }
}

当我将应用程序放在后台时,方法“getLifecycle()”被调用为无限。

1 个答案:

答案 0 :(得分:-1)

解决了多次调用LifecycleOwner.getLifecycle()的问题。我所做的就是评论我的基类中方法的覆盖:

abstract class BaseFragment: dagger.android.support.DaggerFragment(), LifecycleOwner {

var lifeCycleRegistry: LifecycleRegistry

    init {
        AppLogger.d("BaseFragment.init")
        this.lifeCycleRegistry = LifecycleRegistry(this@BaseFragment)
    }

    /*override fun getLifecycle(): Lifecycle {
            AppLogger.d("BaseFragment.getLifecycle and count: ${this.lifeCycleRegistry.observerCount}, ${this.lifeCycleRegistry.currentState}")
            return this.lifeCycleRegistry
        }*/
}

现在它就像一个魅力