wwyt
wwyt

Reputation: 2731

How to achieve one installed app, two launcher icons?

I want to achieve the goal that after installing an application, two icons appear in the Launcher. Clicking on either one of them will launch the corresponding activity. With the XML and JAVA code at the bottom, everything seems to work fine, except one case:

BUG:
Step 1. Click icon 1 to start activity 1
Step 2. Click Home icon to swtich to Launcher
Step 3. From Launcher, click icon 2
Expected result: Activity 2 gets started.
Actual result: Activity 1 gets resumed.

Notice that in step 2, if I click the Back button instead of the Home button to get back to he launcher, then step 3 would succeed. But if I used the Home button, then the bug happens. Could someone please tell me what did I mess? Much appreciate!

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity 
        android:name=".TestActivity1"
        android:label="Test 1">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity 
        android:name=".TestActivity2"
        android:label="Test 2">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>


// The first activity
public class TestActivity1 extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}


// The second activity, it uses a different content view
public class TestActivity2 extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
    }
}

Upvotes: 3

Views: 1448

Answers (1)

K-ballo
K-ballo

Reputation: 81349

Give your activities different task affinities, so they are independent.

Upvotes: 6

Related Questions