JohnyTex
JohnyTex

Reputation: 3481

Is intent.getExtras.getInt() same as intent.getIntExtra()?

I am confused:

Is intent.getExtras.getInt() same as intent.getIntExtra()?

If I start my service using START_REDELIVER_INTENT, will the extras be included in the intent?

I get NullPointerException on restart of my crashed service, which I find strange....

Upvotes: 2

Views: 3321

Answers (2)

ToYonos
ToYonos

Reputation: 16833

From Intent source code :

private Bundle mExtras;

// [...]

public int getIntExtra(String name, int defaultValue) {
    return mExtras == null ? defaultValue :
    mExtras.getInt(name, defaultValue);
}

public Bundle getExtras() {
    return (mExtras != null)
        ? new Bundle(mExtras)
       : null;
}

So yes. Same thing except getExtras() may return null.

Upvotes: 4

Graham Borland
Graham Borland

Reputation: 60681

They are not quite identical. As you are finding out, the first variant will cause a NPE if intent.getExtras() returns null. The second variant does its own null-checking, and returns the default value if the extra is not present.

I can't speculate as to why you aren't getting the expected Extras without seeing more code.

Upvotes: 1

Related Questions