Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of android taskaffinity

I am reading about taskaffinity & created a demo app with following Activities :

  • A
  • B ===> taskaffinity, com.ando
  • C
  • D ===> taskaffinity, com.ando
  • E

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>
like image 249
Akhilesh Mani Avatar asked Jan 27 '14 06:01

Akhilesh Mani


People also ask

What is Android label used for?

android:label represents a label i.e. displayed on the screen. android:name represents a name for the activity class. It is required attribute.

What is the use of activity tag?

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.

What is Android task affinity?

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.

What is back stack in Android?

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.


1 Answers

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.

like image 95
David Wasser Avatar answered Nov 09 '22 22:11

David Wasser