Android -- use activity alias to avoid short cut re creation

Keywords: Android xml

   <activity android:name=".ui.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <meta-data
                android:name="android.app.shortcuts"
                android:resource="@xml/shortcuts"/>
        </activity>

        <activity android:name=".ui.SearchActivity">
        </activity>

Start the main activity page and set shortcuts, which will be the case if the user adds shortcuts to the desktop. There is a search shortcut

If the start page is changed to SearchActivity at this time, the shourcut created by the user in the front page will disappear.

      <activity android:name=".ui.MainActivity">


        </activity>

        <activity android:name=".ui.SearchActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <meta-data
                android:name="android.app.shortcuts"
                android:resource="@xml/shortcuts"/>
        </activity>

The above code changes the startup page to SearchActivity and sets shortcut. You can see that the shortcut added before the homepage has been destroyed after the startup page is changed.

We can avoid this problem through activity alias

   <activity android:name=".ui.MainActivity">
        </activity>
        <activity android:name=".ui.SearchActivity">
        </activity>
        <!--
            //In order to avoid replacing launch actvitiy in the future, resulting in the disappearance of shortcuts,
                //Use activity alias to define launch activity, note that it must be defined in targetActivity
                //after
        -->
        <activity-alias
            android:name=".Launcher"
            android:label="@string/app_name"
            android:targetActivity=".ui.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <meta-data
                android:name="android.app.shortcuts"
                android:resource="@xml/shortcuts"/>
        </activity-alias>

In this way, no matter Android: targetActivity =. UI. Searchactivity or others, the shortcut of the homepage will not be destroyed.

Posted by teo99 on Thu, 30 Apr 2020 13:52:31 -0700