启动Intent for activity-alias [英] Start Intent for activity-alias
问题描述
我为具有不同元数据的活动设置了一些别名。
I set up some Alias for an activity with different meta-data.
在这个元数据中,我设置了片段的名称,然后我通过反射加载。$
我不知道这是否是一个干净的解决方案,虽然通过使用Fragments并将功能放入其中我只有一个SuperActivity和2个空SubActivities来指定清单中的每一个。
In this meta-data I set the name of the fragment which I then load by reflection.
I don't know if that is a "clean" solution, although by using Fragments and putting the functionality inside I had one SuperActivity and 2 Empty SubActivities just to specify each one in the manifest.
现在的问题是:我可以通过Intent启动别名吗? 新的Intent(Context,Class)
将无效,因为我找不到通过intent调用设置元数据的方法。
The question is now: Can I start an alias by an Intent? new Intent(Context, Class)
won't work because I can't find a way to set up the meta-data by an intent call.
我需要通过< activity android:name =XXX/>
开始一个Intent,是否有一个简单的方法那样做?
I would need to start an Intent by <activity android:name="XXX"/>
, is there an easy way to do that?
通用活动及其别名,我需要后者的新意图:
The generic activity and its alias, I need a new Intent of the latter:
<activity
android:name="ActivityList"
android:label="@string/app_name"
android:launchMode="singleTop"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.default_searchable"
android:value="ActivityListItem" />
<meta-data
android:name="fragment"
android:value="FragmentLocationList" />
</activity>
<activity-alias
android:name="ActivityListItem"
android:label="@string/locations"
android:parentActivityName="ActivityList"
android:targetActivity="ActivityList" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
<meta-data
android:name="android.app.default_searchable"
android:value="ActivityListItem" />
<meta-data
android:name="fragment"
android:value="FragmentItemList" />
</activity-alias>
推荐答案
要指定别名,您需要使用 setComponent(ComponentName)
在空的Intent()对象上。
To specify the alias, you need to use setComponent(ComponentName)
on an empty Intent() object.
我倾向于将清单条目设置为相对名称
I tend to set the manifest entry to be a relative name
<activity-alias
android:name=".ActivityListItem"
(注意名称开头的句点),然后使用
(note the period at the beginning of the name), and then form the ComponentName for the Intent using
Intent intent = new Intent();
String packageName = context.getPackageName();
ComponentName componentName = new ComponentName(packageName,
packageName + ".ActivityListItem");
intent.setComponent(componentName);
这篇关于启动Intent for activity-alias的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!