I am currently a bit puzzled about Intent extras.
I know that the so called extras are a Bundle and the Bundle
is internally a map.
And if I put something into this map, with intent.putExtra("someName", "someValue");, I expect that there is now a key called "someName" present in the map. But it isn't, according to my very simple unit test.
public void testIntent(){
assertTrue("true != true", true);
assertFalse("false != false", false);
final String extraName = "IamAnExtra";
final String extraValue = "IamAValue";
Intent intent = new Intent();
intent.putExtra(extraName, extraValue);
assertTrue("hasExtra==false", intent.hasExtra(extraName));
}
Now ths results in the following:
junit.framework.AssertionFailedError: hasExtra==false
What kind of magic is happening behind the intent-code? I know that it is normally working. Is the map cached and written only if I send the intent? Thank you in advance!
Edit:
I added the line assertNotNull("extras == null", intent.getExtras()); which also fails.
And I checked the code from the methods putExtra and getExtras, which is:
public Intent putExtra(String name, String value) {
if (mExtras == null) {
mExtras = new Bundle();
}
mExtras.putString(name, value);
return this;
}
and
public Bundle getExtras() {
return (mExtras != null)
? new Bundle(mExtras)
: null;
}
So if the member mExtras is null, then null is returned for getExtras.
But putExtra will create a new Bundle instance for mExtras, therefore
mExtras should not be null. I'm confused.
Aucun commentaire:
Enregistrer un commentaire