Search code examples
android-intentandroid-manifestandroid-implicit-intentandroid-activity-alias

Use of <activity-alias> to show different launcher for my app


I have a MainActivity in my app. I want my MainActivity through the implicit intent.Also, I want user should have two options to come to my app as I have two cases for both two otpions. I am using as shown below but unable to see two options for my app when user tries to share.

kindly help me to achieve the solution

 <activity
            android:name=".MainActivity"
            android:allowEmbedded="true"
            android:alwaysRetainTaskState="true"
            android:configChanges="layoutDirection|locale"
            android:exported="true"
            android:hardwareAccelerated="true"
            android:launchMode="singleTask"
            android:resizeableActivity="true"
            android:screenOrientation="portrait"
            android:theme="@style/OTPTheme"
            android:windowSoftInputMode="stateVisible|adjustPan">

            <intent-filter android:label="@string/title_activity_main">
                <action android:name="android.intent.action.SEND" />

                <category android:name="android.intent.category.DEFAULT" />

                <data android:mimeType="image/*" android:path="/main_activity" />
            </intent-filter>
        </activity>

        <activity-alias
            android:name=".MyShareActivity"
            android:targetActivity=".MainActivity"
            android:exported="true"
            android:enabled="true">
            <intent-filter android:label="Random Text">
                <action android:name="android.intent.action.SEND" />
                <action android:name="android.intent.action.SEND_MULTIPLE"/>

                <data android:mimeType="image/*" android:path="/share_activity" />
            </intent-filter>
        </activity-alias>

Solution

  • You need to add

    <category android:name="android.intent.category.DEFAULT" />
    

    to the <intent-filter> for the <activity-alias>.

    Here's a note from the documentation for <category>:

    Note: To receive implicit intents, you must include the CATEGORY_DEFAULT category in the intent filter. The methods startActivity() and startActivityForResult() treat all intents as if they declared the CATEGORY_DEFAULT category. If you don't declare it in your intent filter, no implicit intents can resolve your activity.