Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start Intent for activity-alias

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>
like image 338
David Medenjak Avatar asked Feb 27 '14 18:02

David Medenjak


People also ask

When to use activity alias?

The most popular use of activity alias is to prevent the removal of the app launcher shortcut when the underlying launcher activity is changed.

What is activity alias?

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 an independent entity.

Can an activity have multiple intent filters?

To inform the system which implicit intents they can handle, activities, services, and broadcast receivers can have one or more intent filters.

What is the use of activity tag and intent filter tag?

An intent filter is an expression in an app's manifest file that specifies the type of intents that the component would like to receive. For instance, by declaring an intent filter for an activity, you make it possible for other apps to directly start your activity with a certain kind of intent.


1 Answers

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);
like image 119
zmarties Avatar answered Sep 30 '22 14:09

zmarties