4

I have built an Android application with two activities: LoginActivity and RegisterActivity. I need to switch between them using the "a href" tag. So i did this in my AndroidManifest.xml file.

  <activity
        android:name="com.example.test.RegisterActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <category android:name="android.intent.category.DEFAULT" />
            <action android:name="android.intent.action.VIEW" />
            <data android:scheme="com.example.test" />  
        </intent-filter>
    </activity>

In LoginActivity i just added the "a" tag to my string.This will get me to RegisterActivity:

TextView register = (TextView) findViewById(R.id.registerLink);
    register.setText(Html.fromHtml(
            "If you don't have an account " +
            "<a href=\"com.example.test://getApplicationContext\">register</a> "));
    register.setMovementMethod(LinkMovementMethod.getInstance());

So far, it works perfectly. Then i want to turn back in my LoginActivity using also a link: So next, i did this in AndroidManifest file:

 <activity
        android:name="com.example.test.LoginActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
             <category android:name="android.intent.category.DEFAULT" />
                      <data android:scheme="com.example.test" />  
        </intent-filter>
        </activity>
 <activity

And here goes the problem. In RegisterActivity i have the same "href" value and it will get me to my RegisterActivity again although i want to go back to loginActivity.

login = (TextView)findViewById(R.id.loginLink);
    login.setText(Html.fromHtml(
            "If you already have an account please " +
            "<a href=\"com.example.test://getApplicationContext\">login</a> "));
    login.setMovementMethod(LinkMovementMethod.getInstance());

I don't know how to make difference between this two activities. Maybe an id or some parameter will help me but i don't know which one. Thank you in advance!I hope someone can help me.

4

1 回答 1

1

In the interest of completeness (and let's face it, the chance at some sweet rep), I've answered below.

To launch an activity from an <a href= link, you need to:

  1. Make sure a host and scheme is defined in your manifest.xml
  2. Make the correct call in your link-enabled HTML

manifest.xml

<activity android:name=".LoginActivity">
    <intent-filter>
        <data android:host="loginactivity" android:scheme="my-scheme" />
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
    </intent-filter>
 </activity>

Then in code:

login = (TextView)findViewById(R.id.loginLink);
login.setText(Html.fromHtml(
        "If you already have an account please " +
        "<a href=\"my-scheme://loginactivity\">login</a> "));
login.setMovementMethod(LinkMovementMethod.getInstance());

Credit to Sherif elKhatib: (http://www.sherif.mobi/2011/09/html-and-activity-links-in-textview.html)

于 2013-06-15T12:50:22.127 回答