Processing and Parsing XML in Android
In this blog, we will learn about one of Android’s most fascinating topics. Even most of us are unaware of it, despite the fact that it is one of the most essential ideas in Android development. But, before we get into our topic, take out your phones and count the number of apps you have on your smartphone. On average there must be more than 30 applications. However, we only use 5–6 of these 30 apps on a daily basis. Other apps, albeit little utilized, are critical. So, on our home screen or the main screen of our phone, we make shortcuts to commonly used programs. These launchers are used to start the application’s MainActivity or Launcher Activity.
So, in this article, we’ll learn how to make application shortcuts on the home screen? No way, we’re all well aware of it. We are all Android Developers, and we must consider the technical aspects of the platform. As a result, anytime we open a mobile application via a shortcut, the MainActivity or Launcher Activity is invoked. The shortcut’s job is to maintain or save your launcher, and every time you open an app, it will launch the launcher or just MainActivity for you.
However, the problem becomes more complicated when you modify your application’s Launcher Activity. So, try changing your application’s Launcher Activity and running it on your device. Is the shortcut still visible on your home screen? What occurred just now? What happened to the shortcut? Don’t worry, you’ll find out the answers to all of these questions towards the end of this blog. So, in this article, we’ll go over Activity Aliases on Android.
We’ve noticed that if we alter the Launcher Activity of our application, the application’s shortcut on the home screen is gone. But why should the Launcher Activity be changed? The explanation is simple: every time you make a new version for your program, you may need to modify your Launcher Activity owing to new features, or you may have changed the package name and the associated Activity name. As a result, in this situation, the name of your Launcher Activity will be altered but the content stays the same.
Let’s create a project to better understand the situation
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
Now, create a new Activity with the name NewActivity. Below is the code for the activity_new.xml file.
In the NewActivity.kt file, we haven’t written any code. Open the AndroidManifest.xml file now. The following code will be present:
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/gfg_logo" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".New"> </activity> <activity android:name=".Main"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application>
Because we used the intent-filter> in the OldActivity tag, our OldActivity is the Launcher activity. As a result, anytime we run the program, the OldActivity is also launched. Install the app on your smartphone and create a shortcut to it on your home screen. Change the Launcher activity to NewActivity once you’ve created a shortcut. As a result, the following code will be added to our AndroidManifest.xml file:
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/gfg_logo" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".Purani"> </activity> <activity android:name=".Nayi"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application>
Now, launch the app and look for the shortcut you made on the home screen. There are no shortcuts on the home screen.
What is the cause behind this?
When we create a shortcut to a certain application on our home screen, that shortcut remembers the name of the Launcher Activity.
So, to retain the shortcut on the home screen even when the Launcher Activity name changes, we employ the notion of Activity-Alias. The activity-alias > tag is used to launch an Activity while keeping the launchers intact. So what’s stopping you from using the activity alias pal? But how should this Activity-Alias be used? Simply paste the following code into your AndroidManifest.xml file:
<activity android:name=".Purani"/> <activity android:name=".Nayi"/> <activity-alias android:name=".MainActivity" android:targetActivity=".Puranai"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity-alias>
To declare our Launcher Activity, we utilized the activity-alias> tag. See the tags listed below for a better understanding When the Launcher is used, the term “Purani” is used to specify the targeted activity. Create a shortcut to the app on your home screen, then change the Launcher Activity to NewActivity and start it. You can see that even after altering the Launcher Activity, our shortcut remains on the home screen. But what is the cause behind this?
Hence, the name would be remembered by the user. Change the name in the activity-alias> and run the program again. You will encounter an identical issue, in which the shortcut will be deleted from the screen due to a name change.
Geek Tip: To utilize activity-alias>, you must define all of your Activities (including the Launcher Activity) above the activity-alias> tag rather than below it.
Other functionalities made available by activity-alias>
Apart from maintaining the Launcher, the <activity-alias> provides the following features:
<activity-alias android:enabled=["true" | "false"] android:exported=["true" | "false"] android:icon="gfg icon" android:label="string resource" android:name="string" android:permission="string" android:targetActivity="string" > </activity-alias>
- android:enabled: The android: enabled specifies whether or not the targeted activity may be created by the system. If not, the value will be false; otherwise, it will be true. It is true by default for Activity and alias, but in order to start an Activity, both of these values must be true at the same time.
- android:exported: This property indicates whether or not the targetedActivity may be started by components of another application. If it does not, the value is false; otherwise, it is true.
- android:icon: This property specifies the icon for the Targeted Activity that is displayed to the user through an alias. When the alias is displayed to the user.
- android:label: is used to provide a user-readable text for the alias. It is used to uniquely identify an alias by writing a fully classed class name.
- android:permission: The name of the permission required for the alias to launch a certain activity is present here.
- android:targetActivity: This property is used to indicate the name of the Activity that will be started using the alias.
We learned about the notion of activity-alias> in this blog. The <activity-alias> attribute is used to preserve launchers in Android applications. We may modify the Launcher Activity by using the activity-alias>, and our application shortcut will stay in the same location on the home screen.