Android and multiple Intents for an Activity

Android’s Intent resolution can find Activities to act on a datatype. But using this resolution, a single activity can’t consume a datatype in multiple ways.

An application I’m working on has an activity that can consume a datatype in multiple ways. When you think about it, this isn’t an uncommon scenario. For example, within a Twitter application, when an imaginary ComposeTweetActivity is called for a twitter user, you may wish to either send a direct tweet to the user, or a reply to the user. Assuming that our imaginary Twitter application uses the URI structure twitter-user://username to describe a user, putting the following into your AndroidManifest.xml would make sense:

<activity android:name=".ui.ComposeTweetActivity">
    <intent-filter>
        <action android:name="com.sample.twitter.action.TWEET_DIRECT" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.ALTERNATIVE" />
        <category android:name="android.intent.category.SELECTED_ALTERNATIVE" />
        <data android:scheme="twitter-user" />
    </intent-filter>
    <intent-filter>
        <action android:name="com.sample.twitter.action.TWEET_REPLY" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.ALTERNATIVE" />
        <category android:name="android.intent.category.SELECTED_ALTERNATIVE" />
        <data android:scheme="twitter-user" />
    </intent-filter>
</activity>

It would be nice to be able to form a query which would find you both intent-filters so, for example, when a user bought up the context menu of a twitter user, you could show the Direct Message and Reply options.

However, there is no way to do this with the Android API to do this that I know of.

I had hoped originally to use ContextMenu.addIntentOptions() to populate my context menus. Underneath it, this call uses PackageManager.queryIntentActivityOptions() to get the list of intents. However queryIntentActivityOptions() doesn’t find intent-filters, it finds activities with at least one matching intent-filter. Meaning you only get one result per activity. The result also provides no information about the intent-filter that matched your intent.

This is a coherent behaviour – the method finds activities. However, I do think it would have been sensible to support scenarios such as mine, and provide a way to search for intent-filters.

My workaround is to create fake activities for any activity with more than one action, that then hand off to the real activity. The sample manifest I included in the question becomes

<activity android:name=".ui.ComposeTweetActivity" />
<activity android:name=".ui.ComposeTweetActivityDirect">
    <intent-filter>
        <action android:name="com.sample.twitter.action.TWEET_DIRECT" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.ALTERNATIVE" />
        <category android:name="android.intent.category.SELECTED_ALTERNATIVE" />
        <data android:scheme="twitter-user" />
    </intent-filter>
</activity> 
<activity android:name=".ui.ComposeTweetActivityReply">
    <intent-filter>
        <action android:name="com.sample.twitter.action.TWEET_REPLY" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.ALTERNATIVE" />
        <category android:name="android.intent.category.SELECTED_ALTERNATIVE" />
        <data android:scheme="twitter-user" />
    </intent-filter>
</activity>

With ComposeTweetActivityDirect and ComposeTweetActivityReply simply handing off to ComposeTweetActivity with the appropriate action and data.

So you can see how the code works, here’s the onCreateContextMenu code that you’d use on the twitter user list page.

@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
	super.onCreateContextMenu(menu, view, menuInfo);
	AdapterView.AdapterContextMenuInfo info =
			(AdapterView.AdapterContextMenuInfo) menuInfo;
	Intent intent = new Intent(null, getUriOfSelectedUser(info.targetView));
	intent.addCategory(Intent.CATEGORY_SELECTED_ALTERNATIVE);

	// Search and populate the menu with acceptable offering applications.
	menu.addIntentOptions(
			0, // Menu group to which new items will be added
			0, // Unique item ID (none)
			0, // Order for the items (none)
			this.getComponentName(), // The current Activity name
			null, // Specific items to place first (none)
			intent, // Intent created above that describes our requirements
			0, // Additional flags to control items (none)
			null); // Array of MenuItems that correlate to specific items
					// (none)
}

And here’s the code of ComposeTweetActivityDirect (ComposeTweetActivityReply is obviously almost identical).

public class ComposeTweetActivityDirect extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// TODO: No error handling
		Intent sourceIntent = getIntent();		
		Intent newIntent = new Intent(this, ComposeTweetActivity.class);
		newIntent.setAction(ComposeTweetActivity.DIRECT_ACTION_NAME);
		newIntent.setData(sourceIntent.getData());
		startActivity(newIntent);
		finish();
	}
}

And the code in ComposeTweetActivity to receive the intent

Intent intent = getIntent();
String action = intent.getAction();
if (DIRECT_ACTION_NAME.equals(action))
	StartDirectTweetForIntent(intent);
else if (REPLY_ACTION_NAME.equals(action))
	StartReplyForIntent(intent);

At some stage I’d like to deal with in a more elegant fashion, probably by building myself a library, but until that point, I’ll be using this workaround.

The seeds for this post were planted by my Stack Overflow question.

One thought on “Android and multiple Intents for an Activity”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s