'Activity Intent sometimes has null extra
I'm seeing a problem where an Activity crashes grabbing an Intent extra due to it being null
despite not being able to identify anywhere we don't pass that extra (we see it in our crash reporting but can't reproduce). The Activity in question is part of our open source code base, so I can link it here (ErrorActivity.kt), but I'll provide an example of the setup here:
Activity:
class ErrorActivity : Activity() {
companion object {
const val EXTRA_ERRORS = "ERRORS"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val failures = intent.getSerializableExtra(EXTRA_ERRORS) as List<ErrorItem> // This line explodes due to EXTRA_ERRORS being null
}
}
AndroidManifest.xml
<activity android:name="my.code.ErrorActivity"/>
The Activity is pretty simple, no start modes and calling code doesn't use any flags. The only unusual thing is that it's often started from a notification:
val showDetailsIntent = Intent(application, ErrorActivity::class.java).apply {
putExtra(ErrorActivity.EXTRA_ERRORS, results as Serializable)
}
return PendingIntent.getActivity(
application,
101,
showDetailsIntent,
PendingIntent.FLAG_UPDATE_CURRENT
)
We can't find an example where results
above can be null
and I can't identify an Activity recreation scenario where the Intent extras get wiped, another Intent replaces the original passed to the Activity. As far as our tests and our own use of the app are concerned, this problem doesn't occur - we're only able to see it crash reports.
Does anyone know of a scenario that can cause an Activity with extras to "lose" them?
EDIT: In the example given, ErrorItem
is a Kotlin data
class that implements Serializable
.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|