Ben Ben - 1 year ago 99
Android Question

comparing android intent objects

I have 2 android intent objects that can be persisted as URLs and then rehydrated back into intent objects. I'm wondering what is the most effective way to compare any 2 intent objects to ensure that they end up resolving to the same activity with the same parameters etc. Using

does this, but it does not include the extras.

Currently my code for the
method looks like this:

Intent a = Intent.parseUri(this.intentUrl,

Intent b = Intent.parseUri(other.intentUrl,
if (a.filterEquals(b)) {
if (a.getExtras() != null && b.getExtras() != null) {
for (String key : a.getExtras().keySet()) {
if (!b.getExtras().containsKey(key)) {
return false;
} else if (!a.getExtras().get(key)
.equals(b.getExtras().get(key))) {
return false;

// all of the extras are the same so return true
return true;
} else { return false; }

But is there a better/cleaner way?

Answer Source

That's probably as good as it gets, at least conceptually. However, I don't think your algorithm covers cases where b has a key that a does not.

I'd get both keySet() values and run an equals() on those, to confirm they both have the same keys. Then, iterate over one and run equals() on the value pair.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download