activity-alias 解决兼容问题的利器

转载请标明出处:http://blog.csdn.net/EdisonChang/article/details/50458144

历经大半年的重构工作随着版本的快速迭代,已经逐渐趋于稳定。最近遇到一些新老版本的兼容问题,其中有一部分是由于重构后改变了原来activity的包名或者类名,调用方找不到目标activity所致。

且不评论这种按照绝对路径调用activity的劣迹,最为主要的还是如何解决类似此类的兼容问题。

activity-alias,绝对是解决这一类问题的一大利器。activity-alias sdk的说明如下

An alias for an activity, named by the targetActivity attribute. The
target must be in the same application as the alias and it must be
declared before the alias in the manifest. The alias presents the
target activity as a independent entity. It can have its own set of
intent filters, and they, rather than the intent filters on the target
activity itself, determine which intents can activate the target
through the alias and how the system treats the alias. For example,
the intent filters on the alias may specify the
“android.intent.action.MAIN” and “android.intent.category.LAUNCHER”
flags, causing it to be represented in the application launcher, even
though none of the filters on the target activity itself set these
flags.

With the exception of targetActivity, attributes are
a subset of attributes. For attributes in the subset, none
of the values set for the target carry over to the alias. However, for
attributes not in the subset, the values set for the target activity
also apply to the alias.

按sdk叙述内容分析,activity-alias是为 targetActivity 属性命名的 Activity 指定别名。 目标 Activity 必须在当前应用程序中,且必须已在 manifest 中已经声明。
activity-alias将目标 Activity 表示为另一个独立的实体,该实体可以定义 Intent 过滤器,而不使用目标 Activity 已有的过滤器。除 targetActivity 外,< activity-alias > 的属性就是 < activity > 属性的子集,子集可以延用或者重新定义目标 Activity设定的属性。

 alias
            android:name="com.example.test.activities.MainActivity"
            android:targetActivity="com.example.test.home.MainActivity"
            android:exported="true" />

android:name 指定activity的别名(这里用的是重构前的包名+类名),android:targetActivity 指定目标activity,只要在AndroidManifest.xml 为activity声明别名,就可以完美解决兼容问题,不需要加额外代码进行处理。

有任何问题,欢迎补充指正。

你可能感兴趣的:(android)