Reputation: 3552
I am reading about taskaffinity & created a demo app with following Activities :
It is written that, Activities with same taskaffinity secretly opens the single instance of another one.
So, I put log in onResume of every activity to see task id. If it creates single instance then why its not executing onResume of B when I open D and vice-versa.
I read developers site and other post but still not got how to use taskaffinity and whats its use, why we should'tn use singleInstance instead ?
Manifest:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.danroid.taskaffinity.A"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- android:taskAffinity="com.ando" -->
<activity
android:name="com.example.danroid.taskaffinity.B"
android:label="@string/app_name"
android:taskAffinity="@string/task_affinity" >
</activity>
<activity
android:name="com.example.danroid.taskaffinity.C"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.danroid.taskaffinity.D"
android:label="@string/app_name"
android:taskAffinity="@string/task_affinity" >
</activity>
<activity
android:name="com.example.danroid.taskaffinity.E"
android:label="@string/app_name" >
</activity>
</application>
Upvotes: 7
Views: 18230
Reputation: 95626
When you call startActivity()
to transition from one Activity
to another, if you do not set Intent.FLAG_ACTIVITY_NEW_TASK
in the Intent
flags, the new Activity
will be started in the same task, regardless of the value of taskAffinity
.
However, if you set Intent.FLAG_ACTIVITY_NEW_TASK
in the Intent
flags, the new Activity
will still be started in the same task if the new Activity
has the same taskAffinity
as the taskAffinity
of the task (this is determined by the taskAffinity
of the root Activity
in the task). But, if the new Activity
has a different taskAffinity
, the new Activity
will be started in a new task.
Based on your description, if you don't set Intent.FLAG_ACTIVITY_NEW_TASK
when starting a new Activity
, then all of your activities will end up in the same task.
Upvotes: 41