Affinity
AffinityrepresentsActivityexpected to be located in theTask. by default, the sameall of an appActivityall have the sameAffinityvalues. as a result, all of the same applicationActivityThe default is to expect to be in the sameTaskthe. However, you can modifyActivitythe defaultAffinityvalues. in a variety of applicationsActivitycan share the sameAffinityvalues, in the same applicationActivitycan also be given a differentTask Affinityvalues. You can use<activity>Elements oftaskaffinityProperty ModificationActivityof theAffinity,
as shown in the following code. taskaffinityproperty is a string value that must be associated with the<manifest>the package name defined by the element is guaranteed to be unique because the package name is used by the system to identify the application's defaultTask Affinityvalues.
<activity android:name= ". Mainactivity " android:taskaffinity=". Cwp.affinity "><intent-filter> <action android:name=" Android.intent.action.MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </ Intent-filter> </activity>
Affinity will work in the following two scenarios: ?when aActivityof theallowtaskreparentingproperty is set to"true". In this case, when aTaskwhen entering the front desk,Activityof theAffinityis the same as the value, it can be started from theTaskmove into thisTaskthe. Let's verify the situation.
<strong>application Activity taskaffinity allowtaskreparenting </strong>
Application1 Activity1 com.winuxxan.affinity True
we create two projects, Application1and theApplication2, respectively containingActivity1and theActivity2, they aretaskaffinitySame,Activity1of theallowtaskreparentingto betrue. First, we startApplication1,LoadingActivity1, and then pressHomekey to make theTask(assumed to beTask1) into the background. And then startApplication2, the default loadActivity2. What have we seen? Yes, it was supposed to be a show.Activity2, but we saw it.Activity1. ActuallyActivity2Was also loaded, justActivity1re-host, so I saw it.Activity1.
?When you startActivityof theIntentcontains aFLAG
_activity_new_taskflag. By default, a newActivitywill be put into the callstartactivity ()of theActivitywhereTaskand pressed into the caller'sBack Stacktop. However, if passed tostartactivity ()of theIntentcontains aFLAG
_activity_new_taskflag, the system will look for anotherTaskand the newActivityput them in. This is often a new task, but not necessarily. If an existingTaskof theAffinityValue and NewActivitythe same, thenActivitywill put theTask. if not, a newTask. If this flag makesActivitystarted a newTask, the user presses theHomekey to leave, some steps must be taken to allow the user to return to thisTask. some apps, such as the Notification Manager, always letActivityput in otherTaskInstead of putting it in your ownTaskthe. Therefore, they always putFLAG
_activity_NEW
_task
the flag is placed into the intent of the startactivity () . If your activity can be started by an external application with this flag, be aware that the user will return the startup task in other ways , such as by applying an icon (Task The root activity with a category_ launcherintentFilter; see the following section#StartTask).
Let's do a test. We first write an application that has two Activity(Activity1 and Activity2 ), Androidmanifest.xml as follows:
<application android:icon= "@drawable/icon" android:label= "@string/app_name" > <activity android:name= ". Activity1 " android:taskaffinity=" Com.winuxxan.task " android:label=" @string/app_name "> </activity > <activity android:name= ". Activity2 "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/> <category Android : Name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity></application>
Activity2the code is as follows:
public class Activity2 extends Activity { private static final String TAG = "Activity2"; @Override protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.main2); } @Override Public boolean ontouchevent (Motionevent event) { Intent Intent = new Intent (this, activity1.class); C7/>intent.setflags (Intent.flag_activity_new_task); StartActivity (intent); Return Super.ontouchevent (event);} }
then we'll write an application. myactivity, it contains aActivity(myactivity),Androidmanifest.xmlas follows:
<application android:icon= "@drawable/icon" android:label= "@string/app_name" > <activity Android:name= ". MyActivity " android:taskaffinity=" Com.winuxxan.task " android:label=" @string/app_name "> < intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category android:name= " Android.intent.category.LAUNCHER "/> </intent-filter> </activity>
We start first. myactivity, and then pressHomekey, return to the desktop, and then openActivity2, clickActivity2, enterActivity1. Then press the back key. we found that we enteredActivityin the order ofactivity2->activity1, while the return order isactivity1->myactivity. This illustrates a problem,Activity1at startup, it is re-hosted to themyactivitywhere theTask.
Affinity of activity