I have a question regarding objects in kotlin, to which I couldn't find a satisfying answer so far. It's basically the following scenario: I've got some user data, that I want to make available through the entire app, without having to pass it through activities.
To achieve this, I created an object with two properties that are instantiated with the user's data in the startup activity. Is this a safe way to store and make the user's data available for all activities or will it get lost on the way?
Example:
object CurrentUserManager {
lateinit var userId: String,
lateinit var userName: String
}
LoginActivity {
...
onCreate(...){
val user = ApiCall.Login();
CurrentUserManager.userId = user.id
CurrentUserManager.userName = user.name
}
}
MainActivity {
...
onCreate(...){
Toast.makeText(this, "Hello ${CurrentUserManager.userName} with ID: ${CurrentUserManager.userId}", Toast.LENGTH_SHORT).show()
}
}
Is this unsafe/bad practice and if so why and which pattern should I use to achieve the expected outcome?
Thanks, lionthefox