Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Activity Aliases in Android to Preserve Launchers

  • Difficulty Level : Basic
  • Last Updated : 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.

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. \

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!

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






<?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




<?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:

XML




<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:

XML




<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.

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:



XML




<activity android:name=".PrevActivity">

Now, if you will change the name of the Launcher Activity i.e. our name changed to:

XML




<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.

Activity-Alias

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:

XML




<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”:

XML






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: 

XML




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.

Other Features Provided by <activity-alias>

Here are the features provided by the <activity-alias>, apart from preserving the Launcher:

XML




<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>
  1. 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.
  2. 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.
  3. android:icon: It sets the icon for the Targeted Activity that is presented to the user using an alias.
  4. 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.
  5. android:name: It is used to uniquely identify an alias by writing a fully classified class name.
  6. android:permission: Here, the name of the permission is present that is need for a targeted activity to be launched by the alias.
  7. android:targetActivity: It is used to specify the Activity name that is to be launched with the help of the alias.

Conclusion

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!