比较android intent对象 [英] comparing android intent objects

查看:125
本文介绍了比较android intent对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个android intent对象,可以将其持久保存为URL,然后重新水化为intent对象。我想知道最有效的方法是比较任何两个intent对象,以确保它们最终以相同的参数等解析为相同的活动。使用 intent.filterEquals 这样做,但不包括其他功能。

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 intent.filterEquals does this, but it does not include the extras.

当前我的 equals 方法的代码如下:

Currently my code for the equals method looks like this:

            Intent a = Intent.parseUri(this.intentUrl,
                    Intent.URI_INTENT_SCHEME);

            Intent b = Intent.parseUri(other.intentUrl,
                    Intent.URI_INTENT_SCHEME);
            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?

推荐答案

至少从概念上讲,它可能和它一样好。但是,我认为您的算法不能涵盖 b 具有 a 没有的密钥的情况。

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.

我将同时获得 keySet()值并运行 equals(),以确认它们具有相同的密钥。然后,迭代一个并在值对上运行 equals()

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.

这篇关于比较android intent对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆