41

有没有办法以编程方式设置“任务亲和力”?我的意思是像一些意图标志或某事?我在 Android 文档中没有找到任何关于这可能的信息。

在 AndroidManifest.xml 中以静态方式设置亲和力android:taskAffinity不适合我的需要。

4

2 回答 2

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;
 }

并且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;

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

于 2019-08-22T02:12:07.240 回答
-3

使用此方法:

finishAffinity();
于 2018-05-08T07:18:39.660 回答