Android – kotlin: how to check whether a variable with lateinit attribute is initialized

I have a variable declared as

private lateinit var apiDisposable: Disposable

Then in the onpause () method, I'm doing

override fun onPause() {
    super.onPause()
    if (!apiDisposable.isDisposed)
        apiDisposable.dispose()
}

But I see

Can anyone tell me how to check if this variable is initialized? Is there a method like isinitialized()

Any help will be greatly appreciated

resolvent:

Declare your property as a simple property of nullable type:

private var apiDisposable: Disposable? = null

Call this method using secure call notation:

override fun onPause() {
    super.onPause()
    apiDisposable?.dispose()
}

Lateinit reserves the variables used to guarantee initialization, if this is not the case – do not use it

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>