There appears to be a bug in startActivity.
By setting activities to be singleTop with different taskAffinity in AndroidManifest.xml and using the Intent.FLAG_ACTIVITY_NEW_TASK when calling startActivity, two activities can be created in two tasks (one activity per task).
Calling startActivity again will return to the first activity/task and onNewIntent is called. However, calling startActivity a forth time will return to the second activity/task, but onNewIntent is not called.
The only difference between the two tasks is their taskAffinity. Somehow, asymmetrical behaviour is observed.
However, if the Intent.FLAG_ACTIVITY_SINGLE_TOP is also used, then onNewIntent is called as expected.
It would appear that singleTop in AndroidManifest.xml is not the same as Intent.FLAG_ACTIVITY_SINGLE_TOP in the Intent.
public class ActivityA extends Activity implements OnClickListener {
private String tag;
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tag = getClass().getName();
Log.v(tag, "onCreate()");
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.button);
button.setText(tag.endsWith("ActivityA") ? "Activity B"
: "Activity A");
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent;
int flags = Intent.FLAG_ACTIVITY_NEW_TASK
// | Intent.FLAG_ACTIVITY_SINGLE_TOP
;
Log.v(tag, "onClick()");
intent = new Intent(this,
tag.endsWith("ActivityA") ? ActivityB.class
: ActivityA.class);
intent.setFlags(flags);
startActivity(intent);
}
@Override
protected void onNewIntent(Intent intent) {
Log.v(tag, "onNewIntent()");
}
}
public class ActivityB extends ActivityA {
}
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0">
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity
android:name=".ActivityA"
android:launchMode="singleTop"
android:label="Activity A">
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ActivityB"
android:launchMode="singleTop"
android:label="Activity B"
android:taskAffinity="activity.B">
</activity>
</application>
</manifest>