问题描述
最近在开发一个轮胎检测App,当我在两个A --> B Activity 之间跳动时,通过 startActivityForResult 来触发跳转,可是还没有等我结束B,更确切的说是没有跳转到 B,onActivityResult 就收到了消息,这种现象是不可控制的,肯定不是我们期望的。
问题原因
像上面这种情况啊,以我多年开发经验,确诊 这种病源于 你设置了 栈的存放方式,而且设置的还是 Intent.FLAG_ACTIVITY_NEW_TASK 模式
这种模式的意思是 新开一个栈 用于存放 B,也就是说A和B不在一个栈里,那A还等什么B的返回呢!,从跳转B的那刻起,A中的 onActivityResult 就会被触发。
源码分析
public static final int FLAG_ACTIVITY_NEW_TASK = 0x10000000;
/**
* This flag is used to create a new task and launch an activity into it.
* This flag is always paired with either {@link #FLAG_ACTIVITY_NEW_DOCUMENT}
* or {@link #FLAG_ACTIVITY_NEW_TASK}. In both cases these flags alone would
* search through existing tasks for ones matching this Intent. Only if no such
* task is found would a new task be created. When paired with
* FLAG_ACTIVITY_MULTIPLE_TASK both of these behaviors are modified to skip
* the search for a matching task and unconditionally start a new task.
*
* <strong>When used with {@link #FLAG_ACTIVITY_NEW_TASK} do not use this
* flag unless you are implementing your own
* top-level application launcher.</strong> Used in conjunction with
* {@link #FLAG_ACTIVITY_NEW_TASK} to disable the
* behavior of bringing an existing task to the foreground. When set,
* a new task is <em>always</em> started to host the Activity for the
* Intent, regardless of whether there is already an existing task running
* the same thing.
*
* <p><strong>Because the default system does not include graphical task management,
* you should not use this flag unless you provide some way for a user to
* return back to the tasks you have launched.</strong>
*
* See {@link #FLAG_ACTIVITY_NEW_DOCUMENT} for details of this flag's use for
* creating new document tasks.
*
* <p>This flag is ignored if one of {@link #FLAG_ACTIVITY_NEW_TASK} or
* {@link #FLAG_ACTIVITY_NEW_DOCUMENT} is not also set.
*
* <p>See
* <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
* Stack</a> for more information about tasks.
*
* @see #FLAG_ACTIVITY_NEW_DOCUMENT
* @see #FLAG_ACTIVITY_NEW_TASK
/**
* Launch an activity for which you would like a result when it finished.
* When this activity exits, your
* onActivityResult() method will be called with the given requestCode.
* Using a negative requestCode is the same as calling
* {@link #startActivity} (the activity is not launched as a sub-activity).
*
* <p>Note that this method should only be used with Intent protocols
* that are defined to return a result. In other protocols (such as
* {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may
* not get the result when you expect. For example, if the activity you
* are launching uses {@link Intent#FLAG_ACTIVITY_NEW_TASK}, it will not
* run in your task and thus you will immediately receive a cancel result.
*
* <p>As a special case, if you call startActivityForResult() with a requestCode
* >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your
* activity, then your window will not be displayed until a result is
* returned back from the started activity. This is to avoid visible
* flickering when redirecting to another activity.
*
* <p>This method throws {@link android.content.ActivityNotFoundException}
* if there was no Activity found to run the given Intent.
*
* @param intent The intent to start.
* @param requestCode If >= 0, this code will be returned in
* onActivityResult() when the activity exits.
* @param options Additional options for how the Activity should be started.
* See {@link android.content.Context#startActivity(Intent, Bundle)}
* Context.startActivity(Intent, Bundle)} for more details.
*
* @throws android.content.ActivityNotFoundException
*
* @see #startActivity
*/
public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,
@Nullable Bundle options) {
if (mParent == null) {
options = transferSpringboardActivityOptions(options);
Instrumentation.ActivityResult ar =
mInstrumentation.execStartActivity(
this, mMainThread.getApplicationThread(), mToken, this,
intent, requestCode, options);
if (ar != null) {
mMainThread.sendActivityResult(
mToken, mEmbeddedID, requestCode, ar.getResultCode(),
ar.getResultData());
}
if (requestCode >= 0) {
// If this start is requesting a result, we can avoid making
// the activity visible until the result is received. Setting
// this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
// activity hidden during this time, to avoid flickering.
// This can only be done when a result is requested because
// that guarantees we will get information back when the
// activity is finished, no matter what happens to it.
mStartedActivity = true;
}
cancelInputsAndStartExitTransition(options);
// TODO Consider clearing/flushing other event sources and events for child windows.
} else {
if (options != null) {
mParent.startActivityFromChild(this, intent, requestCode, options);
} else {
// Note we want to go through this method for compatibility with
// existing applications that may have overridden it.
mParent.startActivityFromChild(this, intent, requestCode);
}
}
}
英语不好的,把下面的注释拷贝的翻译软件翻译一下,就明白了。
关键注释
* <p>Note that this method should only be used with Intent protocols
* that are defined to return a result. In other protocols (such as
* {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may
* not get the result when you expect. For example, if the activity you
* are launching uses {@link Intent#FLAG_ACTIVITY_NEW_TASK}, it will not
* run in your task and thus you will immediately receive a cancel result.
解决办法
1、直接不要设置这个属性
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ##把你代码里的这句话删掉
2、改为别的属性
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
注意
其实对 Intent.FLAG_ACTIVITY_NEW_TASK 这个属性,是不是一定新开一个栈?
这个问题的答案是 :不一定
假设现在有一个栈1,里面是A,B,C。此时,在C中启动D的时候,设置FLAG_ACTIVITY_NEW_TASK标记,此时会有两种情况:
1.如果D这个Activity在Manifest.xml中的声明中添加了Task Affinity,系统首先会查找有没有和D的Task Affinity相同的Task栈存在,如果有存在,将D压入那个栈
2.如果D这个Activity在Manifest.xml中的Task Affinity默认没有设置,则会把其压入栈1,变成:A B C D,这样就和标准模式效果是一样的了。
也就是说,设置了这个标志后,新启动的Activity并非就一定在新的Task中创建,如果A和B在属于同一个package,而且都是使用默认的Task Affinity,那B还是会在A的task中被创建。 所以,只有A和B的Task Affinity不同时,设置了这个标志才会使B被创建到新的Task。注意如果试图从非Activity的非正常途径启动一个Activity,比如从一个Receiver中启动一个Activity,则Intent必须要添加FLAG_ACTIVITY_NEW_TASK标记。
我们这里之所以会新建一个栈,因为我们的APP和系统Activity的Task Affinity不同