activity-alias : Manage app upgrades, Two Launcher Icons

Alias for an activity which is specified with targetActivity attribute. Target activity should be in same application to that of alias. Target activity has to be declared before alias in manifest file. activity-alias can have its own attributes as we define for activity in manifest. It represents the target activity as an independent entity.

Apart from target Activity attribute, activity-alias attributes are a subset of activity attributes. If alias defines its own attributes, target activity attributes will not be applied. Though for attributes which are declared in target activity and not declared in alias, they will be applied to alias as well.

Alias and target Activity can have different intent-filter and each will behave as different entity even though name attribute of alias does not exist.

[su_box title=”Use of activity-alias” style=”soft” box_color=”#41ee54″ radius=”11″]1. For application upgrade, package structure has changed. can be used to persist applications shortcuts and launcher icons.

2. We need two launcher activities with different icon in our application to take user to different usability/functionality view.

3. If there is minor functionality change between two screen, its better to use alias and use targetActivity rather than rewriting a complete class for alias activity.[/su_box]

Sample activity-alias application

 

[su_button url=”https://github.com/androidsrc/ActivityAlias” target=”blank” style=”stroked” background=”#51d461″ color=”#ffffff” size=”6″ center=”yes” radius=”0″ icon=”icon: arrow-circle-o-down”]Download Full Source Code[/su_button]

 

 [su_carousel source=”media: 234,235,236″ width=”700″ height=”400″ title=”no” mousewheel=”no”]

Let’s define layout for target activity, modify your /res/layout/activity_main.xml as below where we will use text view to show launch info.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.androidsrc.activityaliassamle.MainActivity" >
    <TextView
        android:id="@+id/infoText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="44dp"
        android:textStyle="bold" />

</RelativeLayout>

In AndroidManifest.xml, we will define one launcher component MainActivity as usual. We will define alias AliasActiity for which MainActivity will act as targetActivity. We will set different icon and label for these to differentiate. Target activity must be declared before alias inside application tag.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidsrc.activityaliassamle"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher_main"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:icon="@drawable/ic_launcher_main"
            android:label="MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity-alias
            android:name=".AliasActivity"
            android:icon="@drawable/ic_launcher_alias"
            android:label="AliasActivity"
            android:targetActivity=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity-alias>
    </application>
</manifest>

Defining activity-alias is saving us overhead of creating another class. We just defined targetActivity attribute and MainActivity.java will act host for our alias. In MainActivity.java, we can retrieve component from the intent with which it was started and you will observe different component names. Based upon these, you can modify your functionality and views for respective launch.

package com.androidsrc.activityaliassamle;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

	TextView infoText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Intent intent = getIntent();
		ComponentName cn = intent.getComponent();

		// We can set different content view as well if required based upon
		// ComponentName launched
		setContentView(R.layout.activity_main);

		infoText = (TextView) findViewById(R.id.infoText);

		if (cn.getClassName().contains("AliasActivity")) {
			infoText.setText("Alias Activity CN Launched");
		} else if (cn.getClassName().contains("MainActivity")) {
			infoText.setText("Main Activity CN launched");
		}

	}
}

Run your application you will get two icons on launcher for main activity and activity-alias.

Similarly, we can handle for code package upgrade. Suppose in previous version of application our launcher component was "com.androidsrc.old.MainActivity" and now it has been moved to "com.androidsrc.new.MainActivity". We can use activity-alias for this case. You can delete previous class file without any problem. Just in your manifest, you will create alias which will have target Activity to new class.

        <activity android:name="com.androidsrc.new.MainActivity" />

        <activity-alias
            android:name="com.androidsrc.old.MainActivity"
            android:icon="@drawable/ic_launcher_main"
            android:targetActivity="com.androidsrc.new.MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

Kudos, we just managed to delete old source files and still preserved application shourtcuts on launcher.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *