Use the Intent. hasExtra(String name) to check if an extra with name was passed in the intent. Also, use Intent. getStringExtra(String name) directly on the intent to handle the NullPointerException if no extras were passed.
We can start adding data into the Intent object, we use the method defined in the Intent class putExtra() or putExtras() to store certain data as a key value pair or Bundle data object. These key-value pairs are known as Extras in the sense we are talking about Intents.
putExtra("id".....), I would suggest, when it makes sense, using the current standard fields that can be used with putExtra(), i.e. Intent. EXTRA_something. A full list can be found at Intent (Android Developers).
context. startActivity(new Intent(context, ActivityClass. class)); This call to startActivity sends the Intent to the Android system, which is then in charge of creating and opening the activity that you have specified.
As others have said, both getIntent()
and getExtras()
may return null. Because of this, you don't want to chain the calls together, otherwise you might end up calling null.getBoolean("isNewItem");
which will throw a NullPointerException
and cause your application to crash.
Here's how I would accomplish this. I think it's formatted in the nicest way and is very easily understood by someone else who might be reading your code.
// You can be pretty confident that the intent will not be null here.
Intent intent = getIntent();
// Get the extras (if there are any)
Bundle extras = intent.getExtras();
if (extras != null) {
if (extras.containsKey("isNewItem")) {
boolean isNew = extras.getBoolean("isNewItem", false);
// TODO: Do something with the value of isNew.
}
}
You don't actually need the call to containsKey("isNewItem")
as getBoolean("isNewItem", false)
will return false if the extra does not exist. You could condense the above to something like this:
Bundle extras = getIntent().getExtras();
if (extras != null) {
boolean isNew = extras.getBoolean("isNewItem", false);
if (isNew) {
// Do something
} else {
// Do something else
}
}
You can also use the Intent
methods to access your extras directly. This is probably the cleanest way to do so:
boolean isNew = getIntent().getBooleanExtra("isNewItem", false);
Really any of the methods here are acceptable. Pick one that makes sense to you and do it that way.
You can do this:
Intent intent = getIntent();
if(intent.hasExtra("isNewItem")) {
intent.getExtras().getBoolean("isNewItem");
}
The problem is not the getBoolean()
but the getIntent().getExtras()
Test this way:
if(getIntent() != null && getIntent().getExtras() != null)
myBoolean = getIntent().getExtras().getBoolean("isNewItem");
by the way, if isNewItem
doesn't exist, it returns de default vaule false
.
Regards.
getIntent()
will return null
if there is no Intent
so use...
boolean isNewItem = false;
Intent i = getIntent();
if (i != null)
isNewItem = i.getBooleanExtra("isNewItem", false);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With