Best way to declare member variable in Activity


#1

Noobie to Kotlin here, so this may be a silly question…

I have an Android Activity that is passed a parameter “XYZ” on launch.

In the onCreate method of the Activity, I want to initialise a Member variable of the activity, passing the XYZ parameter to the constructor.

What is the best way to declare this member variable.

Kotlin doesn’t allow me to assign null to it, and I REALLY don’t want to make this member variable nullable. One of the reasons I like Kotlin is the fact that I can create classes that can’t ever be null.

How can I declare the member variable in the knowledge that it can’t be constructed until the onCreate of the Activity.


#2

https://kotlinlang.org/docs/reference/properties.html#late-initialized-properties


#3

Ah… lateinit, solves my problem and very handy indeed.

Thanks for the response. I felt a bit foolish asking the question, but I figured other people might have the same issue.


#4

nice , Here i found the example in kotlin
[https://tutorial.eyehunt.in/android/declare-android-global-variable-kotlin-example/](http://Declare a Android Global Variable in Kotlin)