Can Activity.getIntent() ever return null?

JavaAndroidAndroid Intent

Java Problem Overview


Can Activity.getIntent() ever return null?

The documentation does not mention this as a possibility, so I am wondering if I have to check the result of getIntent() for null value before dereferencing it.

Java Solutions


Solution 1 - Java

Yes, it can, but only in two cases:

In activity constructor:
Intent set up in internal attach method, called from Instrumentation class:

public Activity newActivity(Class<?> clazz, Context context, 
        IBinder token, Application application, Intent intent, ActivityInfo info, 
        CharSequence title, Activity parent, String id,
        Object lastNonConfigurationInstance) throws InstantiationException, 
        IllegalAccessException {
    Activity activity = (Activity)clazz.newInstance();
    ActivityThread aThread = null;
    activity.attach(context, aThread, this, token, 0, application, intent,
            info, title, parent, id,
            (Activity.NonConfigurationInstances)lastNonConfigurationInstance,
            new Configuration(), null, null);
    return activity;
}

therefore intent is always null in constructor.

After setIntent(null):
It's possible to change intent from outside of activity with setIntent().

In all other cases it can't.

Solution 2 - Java

It CAN be null when Your application was updated from the market while it was in the memory and relaunched again after the update. Maybe even If you will make update manually by Studio, or from .apk file, the same effect will be. Not sure, sorry.

I once updated application in Google Dev console and got several different NPE in Crashlitics in the lines with call getIntent(). It happened for all screens, where I used getIntent().getExtra() onCreate or even later in lifeCycle.

So... It looks ugly, but to avoid crashes I need to check intent for NULL value all the time I call getIntent and most of the times I call Finish() if the intent is null. But you can make other logic, ofc, for you purpose.

Solution 3 - Java

Always use onSaveInstanceState() callback method to avoid such situations, that will cause NPE due to null data from getIntentExtra(). Save the extra data to outState bundle and retrieve it on onCreate() callback by putting a null check to confirm if the activity was recreated or was a fresh one.
Example:

if (savedInstanceState != null) {
    extraData = savedInstanceState.getString(EXTRA_DATA);
} else {
    extraData = getIntent().getStringExtra(EXTRA_DATA);
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArmandView Question on Stackoverflow
Solution 1 - JavaKirillView Answer on Stackoverflow
Solution 2 - JavaRelaxedSoulView Answer on Stackoverflow
Solution 3 - JavaRamKrView Answer on Stackoverflow