Reputation: 2027
I'm building an Android app using the Model-View-Presenter design pattern. My app receives a Firebase messaging notification which when my app if not running passes the data into the app (from the notification bar) using an Intent. I have all this working and can see the intent data with this code in the onCreate method:
intent.extras?.let {
for (key in it.keySet()) {
val value = intent.extras?.get(key)
Log.d(TAG, "Key: $key Value: $value")
}
}
I want to save this information to my presenter however and the presenter hasn't been created yet. Can I access intents.extras after onCreate? I tried and got an null pointer exception, so it appears the Intent object gets destroyed after onCreate. Is this the case? How can my presenter get access to this data? My only idea at this point is to create a member variable in the Activity and then save this data to that member variable in onCreate, and then later have the presenter fetch it, but then I'm sort of working against the Model-View-Presenter design pattern of having a dumb view.
Upvotes: 0
Views: 497
Reputation: 1006654
Can I access intents.extras after onCreate?
Yes.
I tried and got an null pointer exception, so it appears the Intent object gets destroyed after onCreate. Is this the case?
No.
However, one risk with Kotlin is if you have a property or variable named intent
— intent.extras
might refer to that property or variable, rather than calling getIntent()
on the activity itself.
Upvotes: 1