Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>My approach cannot work because <code>queryIntentActivityOptions()</code>, and the methods that call it, don't work in the way needed for my approach.</p> <p>For my approach to work, you would need to get a result per intent-filter matched, which could result in multiple results per activity. Also, you would need to get information about which intent-filter matched in the result, specifically the action of the intent-filter.</p> <p>However <code>queryIntentActivityOptions()</code> doesn't find <strong>intent-filters</strong>, it finds <strong>activities</strong> 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.</p> <p>This approach makes sense, but it's a shame that it doesn't allow for an activity to provide multiple ways to consume a particular intent.</p> <p>So my workaround is to create fake activities for any activity with more than one action, that then hand off to the real activity.</p> <p>So the sample manifest I included in the question would become</p> <pre><code>&lt;activity android:name=".ui.MyActivity" android:label="The title" /&gt; &lt;activity android:name=".ui.MyActivityFirstAction" android:label="First action"&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;/activity&gt; &lt;activity android:name=".ui.MyActivitySecondAction" android:label="Second action"&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>MyActivityFirstAction and MyActivitySecondAction would simply call MyActivity with the appropriate action and data.</p> <p>I don't really like this scheme that much, but it still keeps all the actions that are in context menus defined in XML data rather than in code, and allows me to use <code>addIntentOptions()</code>.</p> <p>I still consider <code>addIntentOptions()</code> very tidy, and even if CommonTasks tells me that Google have been backpedaling from it, I will keep using it until I come across issues.</p> <p><strong>EDIT</strong>: As CommonsWare suggests, it would also be possible to create your own library for doing this in a non-hackish fashion. As I end up with more applications, I will probably move in this direction (unless I find an existing method I like better :-) ).</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