Search code examples
androidandroid-intentintentfiltergoogle-photos

Registering 2 activities to receive images, it doesn't work when sharing from Google Photos app


Let's say in my app, users can make posts & send messages to other users, & I want to register my app as a component among those appears in the Share-via picker when sharing either text or an image, I'm defining my 2 activities in my AndroidManifest.xml as follows (like this official example):

<activity 
    android:name=".SharePostActivity"
    android:label="MyApp (Post)">
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
    </intent-filter>
    <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=".ShareMessageActivity"
    android:label="MyApp (Message)">
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="image/*" />
    </intent-filter>
</activity>

When sharing text, this works perfectly fine, where I see 2 icons of my app among available apps for sharing text, named MyApp (Post) & MyApp (Message),

Problem happens when sharing an image, because only one icon appears with the second defined label in the manifest (which is MyApp (Message)), and actually it opens the first defined activity in the manifest (which is SharePostActivity),

So, how to show the 2 options when sharing an image (just like what happens when sharing text)?

(I've tried on an emulator running Nougat & a real device running Oreo)

----- Update -----

I've found that this weird behavior happens only when sharing images from Google's Photos app, but everything works fine when sharing an image from other apps!


Solution

  • Some investigation leads to a solution: each of your intent-filter must contain an android:label which points to a resource, not a hardcoded string.

    ...
    <intent-filter android:label="@string/first_value">
    ...
    

    Different Activities must have different labels, so in your case the Manifest should look like

    <activity 
        android:name=".SharePostActivity"
        android:label="@string/my_app_post">
        <intent-filter
            android:label="@string/my_app_post">
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/plain" />
        </intent-filter>
        <intent-filter
            android:label="@string/my_app_post">
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="image/*" />
        </intent-filter>
    </activity>
    
    <activity
        android:name=".ShareMessageActivity"
        android:label="@string/my_app_message">
        <intent-filter
            android:label="@string/my_app_message">
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/plain" />
        </intent-filter>
        <intent-filter
            android:label="@string/my_app_message">
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="image/*" />
        </intent-filter>
    </activity>