DEV Community

Cover image for Farewell to AliasActivity
Thomas Künneth
Thomas Künneth

Posted on

Farewell to AliasActivity

Google has made a habbit of deprecating things in Android. With API level 30 is was time to say good bye to AliasActivity. Wait, you did not know about that class? Rest assured, you are not alone. 😎 Even though it is gone now (well, sort of, it certainly is still in the codebase), let's take a look at how it was used. And, what you do now.

The idea of the class (whis has been around since API level 1) was to launch

another activity (and then finishes itself) based on information
in its component's manifest meta-data. This is a simple way to
implement an alias-like mechanism. To use this activity, you
should include in the manifest for the associated component an
entry named android.app.alias. It is a reference to an XML
resource describing an intent that launches the real
application.

See the documentation.

<activity android:name=".MainActivity">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
  <meta-data
    android:name="android.app.alias"
    android:resource="@xml/alias" />
</activity>
Enter fullscreen mode Exit fullscreen mode

Above is how that XML file was referenced in the manifest. And this is how the file itself looks:

<?xml version="1.0" encoding="utf-8"?>
<alias xmlns:android="http://schemas.android.com/apk/res/android">
  <intent
    android:targetClass="com.thomaskuenneth.aliasactivitydemo.MainActivity2"
    android:targetPackage="com.thomaskuenneth.aliasactivitydemo" />
</alias>
Enter fullscreen mode Exit fullscreen mode

The source of your alias activity implementation could be as simple as

package com.thomaskuenneth.aliasactivitydemo

import android.app.AliasActivity
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AliasActivity() {
}
Enter fullscreen mode Exit fullscreen mode

whereas the activity that would be called instead is any activity:

package com.thomaskuenneth.aliasactivitydemo

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class MainActivity2 : AppCompatActivity() {

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
  }
}
Enter fullscreen mode Exit fullscreen mode

The source code of this tiny demo is on GitHub.

Now, as the class is deprecated, what do you do instead? There is (by the way, since API level 1) the manifest tag <activity-alias> which is contained in <application>. It is described as an

alias for an activity, named by the targetActivity attribute.
The target must be in the same application as the alias and it
must be declared before the alias in the manifest.

So instead of having

<activity android:name=".MainActivity">
  <intent-filter>
Enter fullscreen mode Exit fullscreen mode

where MainActivity was extending AliasActivity you can now write

<activity-alias android:name=".MainActivity"
  android:targetActivity=".MainActivity2">
<intent-filter>
  <action android:name="android.intent.action.MAIN" />
  <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
Enter fullscreen mode Exit fullscreen mode

Have you used either the class or the manifest tag? If so, why? If not: why? 🤣 Please share your thoughts in the comments.

Top comments (0)