If you'd like to initialize a property outside the constructor, then late-initialized properties is what you may be looking for. Declare the property with the lateinit
modifier, which will allow to skip the otherwise required initializer and will make the property access fail with exception until some meaningful value is assigned to it:
protected lateinit var sharedPreferences: SharedPreferences
Pulling this out of the comments from Alexander Udalov's answer for visibility. For nullable properties:
protected var sharedPreferences : SharedPreferences? = null
...and assign it a value later.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With