mshwf
mshwf

Reputation: 7449

Including two intent-filters, sharing same activity, action, category and mimeType

I'd like to add a share functionality to share photos with my app, the app has two main functionalities, each uses photos as the main input data from the user. So I need to have two share buttons for the two functionalities. I added this in the application tag in the AndroidManifest.xml:

<activity android:name="MainActivity">
  <intent-filter android:label="@string/ENCODE">
    <action android:name="android.intent.action.SEND"/>
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="image/*" />
  </intent-filter>

  <intent-filter android:label="@string/DECODE">
    <action android:name="android.intent.action.SEND" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="image/*" />
  </intent-filter>
</activity >

but only the first intent filter is applied. but when I specified another activity for the second filter, it worked:

<activity android:name="MainActivity" android:label="@string/ENCODE">
  <intent-filter>
    <action android:name="android.intent.action.SEND"/>
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="image/*" />
  </intent-filter>
</activity >

<activity android:name="com.company.myapp.DecodeActivity" android:label="@string/DECODE">
  <intent-filter>
    <action android:name="android.intent.action.SEND" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="image/*" />
  </intent-filter>
</activity >

but I had to create DecodeActivity and copy the setup code from the MainActivity, this for example caused problems like they share some resources (photo picker) that gets ambiguous between activities and navigate from the DecodeActivity to the MainActivity, because it initially uses the MainActivity.

Here I'm resolving the incoming intent:

    if (Intent.ActionSend.Equals(Intent.Action) && Intent.Type != null && Intent.Type.StartsWith("image/"))
    {
        await ImageDispatcher.HandleSendImage(ContentResolver, Intent, "Encode");
    }

Upvotes: 1

Views: 1096

Answers (1)

Rediska
Rediska

Reputation: 1470

The two intent filters differ only by the label, which does not affect the function of the filter in any way. I think you should have two activities (one for each operation), and both should have the same base class. Put the operation-specific code in the operation-specific activities. Alternatively, you may ask the user what to do with the image.

Upvotes: 2

Related Questions