Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd to menu using addIntentOptions providing multiple intents for a single activity
    text
    copied!<p>I want to use <code>addIntentOptions</code> to drive my menus when ever possible. This seems the cleanest way to provide them. Rather than explicitly detailing activities, simply ask for a menu listing all the activities which are available for my data item.</p> <p>So I'm trying to put together a context menu for a <code>ListView</code>. It works great. Only problem is that I have an activity that has two intents that consume my data type, and only the first shows up.</p> <p>The activity in question in <code>AndroidManifest.xml</code></p> <pre><code>&lt;activity android:name=".ui.MyActivity" android:label="The title"&gt; &lt;intent-filter android:label="First context label"&gt; &lt;action android:name="com.sample.action.FIRST_ACTION" /&gt; &lt;category android:name="android.intent.category.DEFAULT" /&gt; &lt;category android:name="android.intent.category.ALTERNATIVE" /&gt; &lt;category android:name="android.intent.category.SELECTED_ALTERNATIVE" /&gt; &lt;data android:scheme="myscheme" /&gt; &lt;/intent-filter&gt; &lt;intent-filter android:label="Second context label"&gt; &lt;action android:name="com.sample.action.SECOND_ACTION" /&gt; &lt;category android:name="android.intent.category.DEFAULT" /&gt; &lt;category android:name="android.intent.category.ALTERNATIVE" /&gt; &lt;category android:name="android.intent.category.SELECTED_ALTERNATIVE" /&gt; &lt;data android:scheme="myscheme" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; </code></pre> <p>The code to generate the context menu</p> <pre><code>@Override public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, view, menuInfo); Uri uri = Uri.fromParts("myscheme", getOpaqueUriOfSelectedItem(view), null) Intent intent = new Intent(null, uri); 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) } </code></pre> <p>As I say, the first intent of the activity shows up in the context menu and behaves like a dream. But I don't see the second intent, and I see no good reason it shouldn't show up. If Android only allows one intent with a particular category per activity, that's a pretty lame restriction.</p> <p>I can see myself building a dummy activity that simply hands off to <code>MyActivity</code>. But that's clumsy and I'd like to avoid it if possible.</p> <p><strong>EDIT</strong>: Looking at the intent that is passed through to an activity from a context menu (or option menu, presumably), even if both intents showed up in the menu, the activity wouldn't have enough information to tell which intent was selected, as within the activity <code>getIntent().getAction()</code> is null. </p> <p>This seems like an unfortunate oversight. Surely it isn't that unusual to have an activity that can consume a type of data in more than one way?</p> <p>Unless one of you kind folk know something I've missed, it looks like I'm going to be creating my dummy activities.</p> <p><strong>EDIT</strong>: As CommonsWare suggested, I tried using <code>queryIntentActivityOptions</code>. I added in this code before <code>menu.addIntentOptions</code> in my code above.</p> <pre><code>PackageManager pm = getPackageManager(); final List&lt;ResolveInfo&gt; available = pm.queryIntentActivityOptions(this.getComponentName(), null, intent, 0); </code></pre> <p>And in the debugger I found that <code>available</code> didn't include both of the available intents for <code>MyActivity</code>. So the issue isn't within <code>addIntentOptions</code>, it's deeper, within <code>queryIntentActivityOptions</code> somewhere.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload