3

According to documentation AliasActivity stub can be used for choosing which activity to start. AliasActivity is just ordinar activity, what it does is just parse android.app.alias tag to use xml file for start the activity. In android manifest file it can be used like:

<activity
    android:name="android.app.AliasActivity"
    android:label="@string/app_name" >
    <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>

After searching I've found that alias.xml may looks like:

<?xml version="1.0" encoding="utf-8"?>

<alias xmlns:android="http://schemas.android.com/apk/res/android">
    <intent
        android:targetPackage="com.example.aliasexample"
        android:targetClass="com.example.aliasexample.MainActivity"
        android:data="http://www.google-shmoogle.com/">
    </intent>

</alias>

So how to use AliasActivity. I am not just finding the solution, I want to find elegant one, and trying to use for that AliasActivity. For example, I should start one activity in case of some shared preference set and another one if not set. Is it possible to use AliasActivity for that or it can be just used for some device-specific characteristics, that we specify in xml? So what is the reason of using AliasActivity?

And second question is can I get access to some sharedPreference value directly in xml?

Lazy Beard
  • 305
  • 3
  • 10

1 Answers1

0
  1. To open different activities based on a SharedPreference Value, you can use the regular Activity Class. Inside onCreate() just check for the required valued and start another activity from there and call finish() in the first activity. It'll appear as if the first activity was not there.

  2. An alias activity tag is used to define multiple launcher activities. You'll see multiple app icons for your app, depending on your manifest. Example Here.

Edit 1: Couldn't find an implementation of AliasActivity at any place but one. Adding Huge Source Code. Self Explanatory.

/**
 * Stub activity that launches 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.
 */
public class AliasActivity extends Activity {
    /**
     * This is the name under which you should store in your component the
     * meta-data information about the alias.  It is a reference to an XML
     * resource describing an intent that launches the real application.
     * {@hide}
     */
    public final String ALIAS_META_DATA = "android.app.alias";

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

        XmlResourceParser parser = null;
        try {
            ActivityInfo ai = getPackageManager().getActivityInfo(
                    getComponentName(), PackageManager.GET_META_DATA);
            parser = ai.loadXmlMetaData(getPackageManager(),
                    ALIAS_META_DATA);
            if (parser == null) {
                throw new RuntimeException("Alias requires a meta-data field "
                        + ALIAS_META_DATA);
            }

            Intent intent = parseAlias(parser);
            if (intent == null) {
                throw new RuntimeException(
                        "No <intent> tag found in alias description");
            }

            startActivity(intent);
            finish();

        } catch (PackageManager.NameNotFoundException e) {
            throw new RuntimeException("Error parsing alias", e);
        } catch (XmlPullParserException e) {
            throw new RuntimeException("Error parsing alias", e);
        } catch (IOException e) {
            throw new RuntimeException("Error parsing alias", e);
        } finally {
            if (parser != null) parser.close();
        }
    }

    private Intent parseAlias(XmlPullParser parser)
            throws XmlPullParserException, IOException {
        AttributeSet attrs = Xml.asAttributeSet(parser);

        Intent intent = null;

        int type;
        while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
                && type != XmlPullParser.START_TAG) {
        }

        String nodeName = parser.getName();
        if (!"alias".equals(nodeName)) {
            throw new RuntimeException(
                    "Alias meta-data must start with <alias> tag; found"
                    + nodeName + " at " + parser.getPositionDescription());
        }

        int outerDepth = parser.getDepth();
        while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
               && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
            if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
                continue;
            }

            nodeName = parser.getName();
            if ("intent".equals(nodeName)) {
                Intent gotIntent = Intent.parseIntent(getResources(), parser, attrs);
                if (intent == null) intent = gotIntent;
            } else {
                XmlUtils.skipCurrentTag(parser);
            }
        }

        return intent;
    }

}
Community
  • 1
  • 1
Shivam Verma
  • 7,973
  • 3
  • 26
  • 34