'Lazy initialized objects are always created
In some case my broadcast receiver is not required and so need to check if the receiver is null or not but somehow this object is not null even if not using it and causing crash.
private val myBroadCastReceiver by lazy { MyBroadcastReceiver() } if(myBroadCastReceiver != null) unregisterReceiver(myBroadCastReceiver)
Solution 1:[1]
Because you declare myBroadcastReceiver as Lazy, that means that you won't use it until you call MyBroadcastReceiver(). Which you do in your if statement.
So if you check it that way, it won't be null, because you actually execute MyBroadcastReceiver() here if(myBroadCastReceiver...)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
