Activity Aliases in Android to Preserve Launchers
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.
Before Moving Forward
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:
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:
We have not written any code in the NewActivity.kt file. Now, open the AndroidManifest.xml file. The following code will be there:
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:
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.
What’s the reason behind this?
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:
Now, if you will change the name of the Launcher Activity i.e. our name changed to:
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:
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”:
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:
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.
Other Features Provided by <activity-alias>
Here are the features provided by the <activity-alias>, apart from preserving the Launcher:
- 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.