Farewell to AliasActivity
Tags: android
Published: 2021-01-30 14:41:43

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](https://developer.android.com/reference/android/app/AliasActivity).

```xml
<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>
```

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

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

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

```kotlin
package com.thomaskuenneth.aliasactivitydemo

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

class MainActivity : AliasActivity() {
}
```

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

```kotlin
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)
  }
}
```

The source code of this tiny demo is on [GitHub](https://github.com/tkuenneth/android-demos/tree/master/misc/AliasActivityDemo).

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](https://developer.android.com/guide/topics/manifest/activity-alias-element) 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

```xml
<activity android:name=".MainActivity">
  <intent-filter>
```

where `MainActivity` was extending `AliasActivity` you can now write

```xml
<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>
```

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