Understanding Activity Aliases in Android To Preserve Your Launchers

Understanding Activity Aliases in Android To Preserve Your Launchers

In this blog, we will learn one of the interesting topics of Android. Even most of us don’t know or haven’t heard about it and trust me it is one of the important concepts of Android Development. So, the topic of this blog is “ Understanding Activity Aliases in Android ”. But before moving on to our topic, put up your mobile phones and count the number of applications that you are having in your device. I am having 87 :) Yeah tell me yours. I think all of you must have more than 30 applications on an 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, we create shortcuts of frequently used applications on our home screen or on our phone’s main screen. These launchers are used to launch the MainActivity or the Launcher Activity of that particular application.

Hey, hold on! So, in this blog, we will learn how to create shortcuts of application on the home screen? No way, we all know it very well. We all are Android Developers and we should look upon the technical part of it. So, whenever we launch a mobile application from the shortcuts, then the MainActivity or 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 MainActivty as OldActivity (you can use other name also). Following is the code for my activity_old.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=".OldActivity">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World! I am in Old 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 OldActivity.kt file.

Now create another Activity with activity name as NewActivity . The code for the activity_new.kt 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. 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=".OldActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

Here, our OldActivity is the Launcher activity because we have used the <intent-filter> in the OldActivtiy tag. So, whenever we will launch the application, then the OldActivity 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=".OldActivity">
    </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 shortcut 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 OldActivity:

<activity android:name=".OldActivity">

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 as OldActivity 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 Activtiy-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=".OldActivity"/>
<activity android:name=".NewActivity"/>
<activity-alias
        android:name=".MainActivity"
        android:targetActivity=".OldActivity">
    <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 OldActivity will be launched because the android:targetActivity=”.OldActivity” 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=".OldActivity"

So, change the targetActivity to your choice and keep the name same in the <activity-alias>.

Now, try to change the name in the <activty-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 form 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>
  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 application. 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.

That’s it for this blog. See you in the next blog.

Till then, Keep Learning :)

Team MindOrks!