以编程方式设置"任务关联"

aMi*_*iGo 25 android manifest affinity

有没有办法以编程方式设置"任务亲和力"?我的意思是喜欢一些Intent旗帜或某事?我没有在Android文档中找到任何关于这一点的内容.

使用"android:taskAffinity"在AndroidManifest.xml中以静态方式设置关联不符合我的需要.

Sta*_*Kou 5

你不能。

taskAffinity由 包含ActivityInfo,它是 的成员Activity

活动源代码

public class Activity extends ContextThemeWrapper
    ...
    ... {
    // set by the thread after the constructor and before 
    // onCreate(Bundle savedInstanceState) is called.

    @UnsupportedAppUsage
    /*package*/ ActivityInfo mActivityInfo;
 }
Run Code Online (Sandbox Code Playgroud)

并且ActivityInfo具有 taskAffinity。

ActivityInfo 的源代码

/**
 * Information you can retrieve about a particular application
 * activity or receiver. This corresponds to information collected
 * from the AndroidManifest.xml's <activity> and
 * <receiver> tags.
 */
public class ActivityInfo extends ComponentInfo implements Parcelable {

   /**
    * The affinity this activity has for another task in the system.  The
    * string here is the name of the task, often the package name of the
    * overall package.  If null, the activity has no affinity.  Set from the
    * {@link android.R.attr#taskAffinity} attribute.
    */
    public String taskAffinity;
Run Code Online (Sandbox Code Playgroud)

根据源代码的注释,信息taskAffinity是从AndroidManifest.xml. 并且没有可以设置的公共方法mActivityInfo.taskAffinity


Pru*_*ure -4

使用这个方法:

finishAffinity();
Run Code Online (Sandbox Code Playgroud)