Processing and Parsing XML in Android
Last Updated :
19 Sep, 2021
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.
Before Proceeding
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.
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=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Geeks for Geeks Old Activity"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Now, create a new Activity with the name NewActivity. Below is the code for the activity_new.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=".NewActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Geeks for Geeks New Activity"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
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.
Activity-Alias
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.
Conclusion
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.
Similar Reads
XML Parsing in Android using SAX Parser
Generally, XML (Extensible Mark-up Language) is a commonly used data exchange format to interchange servers' data. In Android, SAX stands for Simple API for XML and is a widely used API for XML parsing. Like the DOM parser, the SAX parser is also used to perform in-memory operations to parse the XML
6 min read
XML Parsing in Android using DOM Parser
Android DOM(Document Object Model) parser is a program that parses an XML document and extracts the required information from it. This parser uses an object-based approach for creating and parsing the XML files. In General, a DOM parser loads the XML file into the Android memory to parse the XML doc
6 min read
XML Parsing in Android using XmlPullParser
In android, the XMLPullParser interface provides the functionality to parse the XML files in android applications. The XMLPullParser is a simple and efficient parser method to parse the XML data when compared to other parser methods such as DOM Parser and SAX Parser. The XMLPullParser has a method n
5 min read
XML Namespaces in Android
In an XML file, namespaces are used to provide elements and attributes with distinctive names. Names for elements or attributes in an XML instance may come from different XML vocabularies. The ambiguity between similar elements or attributes can be eliminated if each vocabulary is given its own name
2 min read
JSON Parsing in Android
JSON (JavaScript Object Notation) is a straightforward data exchange format to interchange the server's data, and it is a better alternative for XML. This is because JSON is a lightweight and structured language. Android supports all the JSON classes such as JSONStringer, JSONObject, JSONArray, and
4 min read
JSON Parsing in Android using Volley Library
JSON is also known as (JavaScript Object Notation) is a format to exchange the data from the server. The data stored in JSON format is lightweight and easy to handle. With the help of JSON, we can access the data in the form of JsonArray, JsonObject, and JsonStringer. In this article, we will specif
6 min read
JSON Parsing in Android using Retrofit Library
JSON is also known as (JavaScript Object Notation) is a format to exchange the data from the server. The data stored in JSON format is lightweight and easy to handle. With the help of JSON, we can access the data in the form of JsonArray, JsonObject, and JsonStringer. In this article, we will specif
6 min read
What is Uri.parse() in Android?
Uri stands for Uniform Resource Identifier. Uri is the sequence of the characters used to identify resources uniquely over the internet. In this article, we are going to see what is Uri.parse() method in Android. Step-by-Step Implementation Step 1: Create a New Project in Android Studio To create a
2 min read
Android Coding Style and Guidelines
While you writing code, there are some rules you need to follow to keep code easy to read and clean. If you are not new to coding, you probably already know about coding standards. Coding standards vary from person to person and can be different for different projects. Before starting adding or upda
5 min read
How to Add Memes Using API Call in Android?
Application Programming Interface calling is the process of making requests to external web-based services to retrieve or manipulate data. APIs provide a standardized way for different software applications to communicate with each other. It involves sending a request from one application to another
4 min read