Skip to content
Advertisement

In Activity.onCreate(), why does Intent.getExtras() sometimes return null?

This was probably a false alarm, see my own answer. Original question below:

An activity has a button that takes the user to another activity. To launch the new activity, we populate our Intent with extras, and onCreate(), the new activity reads from those extras via Intent.getExtras(). We assumed the returned bundle would be non-null, but as customer crash reports discovered, getExtras() sometimes returns null.

Null-guarding the extras, as this answer shows, is perfectly fine, but if you populate the intent’s extras, then why would it ever return null later? Is there a better place (like onResume()) to read the extras?

EDIT: It may be because we are not following the name convention required for the keys:

The name must include a package prefix, for example the app com.android.contacts would use names like “com.android.contacts.ShowAll”.

This is from the Intent.putExtras javadoc. What happens if you don’t follow this name convention; is the behavior even defined?

Here’s the relevant code:

JavaScript

Here is a sample stack trace of this problem:

JavaScript

Advertisement

Answer

This was probably a false alarm; it’s more likely due to our own instance variable being null:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement