启动Intent for activity-alias

时间:2014-02-27 18:00:25

标签: java android android-intent

我为具有不同元数据的活动设置了一些别名。

在这个元数据中,我设置了片段的名称,然后我通过反射加载 我不知道这是否是一个“干净”的解决方案,尽管通过使用Fragments并将功能放入其中我只有一个SuperActivity和2个Empty SubActivities来指定清单中的每个。

现在的问题是:我可以通过Intent启动别名吗? new Intent(Context, Class)无效,因为我找不到通过意向调用设置元数据的方法。

我需要通过<activity android:name="XXX"/>启动一个Intent,有一个简单的方法吗?

泛型活动及其别名,我需要后者的新意图:

    <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>

1 个答案:

答案 0 :(得分:19)

要指定别名,您需要在空的Intent()对象上使用setComponent(ComponentName)

我倾向于将清单条目设置为相对名称

<activity-alias
    android:name=".ActivityListItem"

(注意名称开头的句点),然后使用

形成Intent的ComponentName
Intent intent = new Intent();
String packageName = context.getPackageName();
ComponentName componentName = new ComponentName(packageName,
                                                packageName + ".ActivityListItem");
intent.setComponent(componentName);