Activity Aliases in Android to Preserve Launchers - GeeksforGeeks

21 Nov, 2021
Before moving on to the topic, pick up your mobile phones and count the number of applications that you are having on your device. All of you must have more than 30 applications on average. But out of these 30 apps, we only use 5–6 applications on a regular basis. Other applications are rarely used but are important. So, what we do is, create shortcuts of frequently used applications on our home screen or on our phone’s main screen. These launchers are used to launch the Launcher Activity of that particular application. Hold on! So, in this blog, we will learn how to create shortcuts for applications on the home screen? No way, we all know it very well. So, whenever we launch a mobile application from the shortcuts, then the Launcher Activity is called. The duty of the shortcut is to keep or store your launcher and whenever you start an app then the shortcut will launch the launcher or simply MainActivity for you. But the situation gets more tricky when you change the Launcher Activity of your application. So, try to change the Launcher Activity of your application and run the application on your device. Do you still find the shortcut on the home screen? Hey, what has just happened? Where has the shortcut gone? Don’t worry, at the end of this blog, you will come to know the answers to all these questions. So, in this blog, we will learn about Activity Alias in Android. Let’s get started.
We have seen that, if we change the Launcher Activity of our application, then the shortcut of the application on the home screen will be lost. But why should anyone change the Launcher Activity? The reason is very simple, whenever you are making a new update for your application, there may be situations where you may have to change your Launcher Activity due to some new features or there may be situations where you have changed the package name and the corresponding Activity name. So, in this case, also, the name of your Launcher Activity will be changed while the content remains the same. \
Let’s make a project to understand the problem in a better way. Create a project in Android Studio and name your MainActivity as PrevActivity (you can use another name also). Following is the code for my activity_prev.xml file:
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout        xmlns:android="http://schemas.android.com/apk/res/android"        xmlns:tools="http://schemas.android.com/tools"        xmlns:app="http://schemas.android.com/apk/res-auto"        android:layout_width="match_parent"        android:layout_height="match_parent"        tools:context=".PrevActivity">     <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Hello World! I am in Previous Activity"            android:textSize="24sp"            app:layout_constraintBottom_toBottomOf="parent"            app:layout_constraintLeft_toLeftOf="parent"            app:layout_constraintRight_toRightOf="parent"            app:layout_constraintTop_toTopOf="parent"/> </androidx.constraintlayout.widget.ConstraintLayout>
We don’t need to add any code for the PrevActivity.kt file. Now create another Activity with activity name as NewActivity. The code for the activity_new.xml is:
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout        xmlns:android="http://schemas.android.com/apk/res/android"        xmlns:tools="http://schemas.android.com/tools"        xmlns:app="http://schemas.android.com/apk/res-auto"        android:layout_width="match_parent"        android:layout_height="match_parent"        tools:context=".NewActivity">     <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Hello World! I am in New Activity"            android:textSize="24sp"            app:layout_constraintBottom_toBottomOf="parent"            app:layout_constraintLeft_toLeftOf="parent"            app:layout_constraintRight_toRightOf="parent"            app:layout_constraintTop_toTopOf="parent"/> </androidx.constraintlayout.widget.ConstraintLayout>
We have not written any code in the NewActivity.kt file. Now, open the AndroidManifest.xml file. The following code will be there:
<application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:roundIcon="@mipmap/ic_launcher_round"        android:supportsRtl="true"        android:theme="@style/AppTheme">    <activity android:name=".NewActivity">    </activity>    <activity android:name=".PrevActivity">        <intent-filter>            <action android:name="android.intent.action.MAIN"/>             <category android:name="android.intent.category.LAUNCHER"/>        </intent-filter>    </activity></application>
Here, our PrevActivity is the Launcher activity because we have used the <intent-filter> in the PrevActivity tag. So, whenever we will launch the application, then the PrevActivity will be launched. Now, install the application on your device and make a shortcut of the app on your home screen. After creating a shortcut, change the Launcher activity to NewActivity. So, the code of our AndroidManifest.xml file will be changed to:
<application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:roundIcon="@mipmap/ic_launcher_round"        android:supportsRtl="true"        android:theme="@style/AppTheme">    <activity android:name=".PrevActivity">    </activity>    <activity android:name=".NewActivity">        <intent-filter>            <action android:name="android.intent.action.MAIN"/>             <category android:name="android.intent.category.LAUNCHER"/>        </intent-filter>    </activity></application>
Now, run the app and try to find the shortcut that you have created on the home screen. You will not find any shortcuts on the home screen.
Whenever we make a shortcut of a particular application on our home screen then that shortcut remembers the name of the Launcher Activity i.e. in our example the name is PrevActivity:
<activity android:name=".PrevActivity">
Now, if you will change the name of the Launcher Activity i.e. our name changed to:
<activity android:name=".NewActivity">
The problem arises here, the shortcut is having the name PrevActivity but now the name has changed to NewActivity and it gets confused and the shortcut is deleted from the home screen.
So, in order to keep the shortcut on the home screen, even after the change in the Launcher Activity name, we use the concept of Activity-Alias. The <activity-alias> is used to launch an Activity by preserving the launchers. So, by using the <activity-alias> you can change your Launcher Activity and the shortcut launcher will also be preserved on the home screen. But how to use this Activity-Alias? Just use the below code in your AndroidManifest.xml file:
<activity android:name=".PrevActivity"/><activity android:name=".NewActivity"/><activity-alias        android:name=".MainActivity"        android:targetActivity=".PrevActivity">    <intent-filter>        <action android:name="android.intent.action.MAIN" />         <category android:name="android.intent.category.LAUNCHER" />    </intent-filter></activity-alias>
Here, we have used the <activity-alias> tag to declare our Launcher Activity. Whenever the application will be launched, then the PrevActivity will be launched because the android:targetActivity=”.PrevActivity” is used to define the targeted activity when the Launcher is called. 
Now, make a shortcut of the application on your home screen and then change the Launcher Activity to NewActivity and run the app. Now, you can see that after changing the Launcher Activity, our shortcut is still there on the home screen. But what’s the reason behind this?
So, in our case when you make a shortcut of our application, then the name will be remembered i.e. in our case “MainActivity”:
android:name=".MainActivity"
So, whenever the Launcher will be called, then the shortcut will search for the name “MainActivity” and if it finds the same then it will launch the Activity that is written in: 
android:targetActivity=".PrevActivity"
So, change the target activity to your choice and keep the name the same in the <activity-alias>. Now, try to change the name in the <activity-alias> and run the application. You will find the same problem i.e. the shortcut will be removed from the screen because the name has been changed. 
Note: In order to use <activity-alias> you need to declare all your Activities (including the Launcher Activity) above the <activity-alias> tag and not below the <activity-alias> tag.
Here are the features provided by the <activity-alias>, apart from preserving the Launcher:
<activity-alias android:enabled=["true" | "false"]                android:exported=["true" | "false"]                android:icon="drawable resource"                android:label="string resource"                android:name="string"                android:permission="string"                android:targetActivity="string" >    . . .</activity-alias>
android:enabled: The android: enabled is used to tell whether the targeted activity can be instantiated by the system or not. If not then the value will be false otherwise true. By default, it is true for Activity and alias but in order to launch an Activity, both these values must be true at a time.android:exported: It is used to tell whether or not the targetedActivity can be launched by the components of other applications. If not, then the value will be false, otherwise, it is true.android:icon: It sets the icon for the Targeted Activity that is presented to the user using an alias.android:label: When the alias is presented to the user then this android:label is used to set a user-readable text for the alias.android:name: It is used to uniquely identify an alias by writing a fully classified class name.android:permission: Here, the name of the permission is present that is need for a targeted activity to be launched by the alias.android:targetActivity: It is used to specify the Activity name that is to be launched with the help of the alias.
android:enabled: The android: enabled is used to tell whether the targeted activity can be instantiated by the system or not. If not then the value will be false otherwise true. By default, it is true for Activity and alias but in order to launch an Activity, both these values must be true at a time.
android:exported: It is used to tell whether or not the targetedActivity can be launched by the components of other applications. If not, then the value will be false, otherwise, it is true.
android:icon: It sets the icon for the Targeted Activity that is presented to the user using an alias.
android:label: When the alias is presented to the user then this android:label is used to set a user-readable text for the alias.
android:name: It is used to uniquely identify an alias by writing a fully classified class name.
android:permission: Here, the name of the permission is present that is need for a targeted activity to be launched by the alias.
android:targetActivity: It is used to specify the Activity name that is to be launched with the help of the alias.
I hope that you have learned something new in this blog. Let’s recap. In this blog, we learned the concept of <activity-alias>. The <activity-alias> is used to preserver launchers in the Android Application. By using the <activity-alias>, we can change the Launcher Activity and our shortcut of the application will remain at the same place on the home screen.
sagar0719kumar
gulshankumarar231
Picked
TrueGeek-2021
Android
TrueGeek
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Resource Raw Folder in Android Studio
Flutter - Custom Bottom Navigation Bar
How to Read Data from SQLite Database in Android?
How to Post Data to API using Retrofit in Android?
Retrofit with Kotlin Coroutine in Android
How to redirect to another page in ReactJS ?
How to remove duplicate elements from JavaScript Array ?
Basics of API Testing Using Postman
SQL Statement to Remove Part of a String
Types of Internet Protocols