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>
android:label represents a label i.e. displayed on the screen. android:name represents a name for the activity class. It is required attribute.
You can use the manifest's <activity> tag to control which apps can start a particular activity. A parent activity cannot launch a child activity unless both activities have the same permissions in their manifest.
An affinity indicates which task an activity prefers to belong to. By default, all the activities from the same app have an affinity for each other. So, by default, all activities in the same app prefer to be in the same task. However, you can modify the default affinity for an activity.
Android defines the unit of a sequence of user interactions as Task. A Task is a collection of activities that user interact when performing a certain job. A Task holds the Activities, arranged in a Stack called Back Stack. The Stack has LIFO structure and stores the activities in the order of their opening.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With