Start Intent for activity-alias

2020-03-23 17:58发布

I set up some Alias for an activity with different meta-data.

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.

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.

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>

1条回答
劫难
2楼-- · 2020-03-23 18:07

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);
查看更多
登录 后发表回答