【问题标题】:Two questions about the launch mode in Android关于Android中启动模式的两个问题
【发布时间】:2017-06-25 15:26:46
【问题描述】:

当我在Activity1 中使用startActivityForResult() 启动Activity2 时,如果Activity1Activity2 不在同一个任务堆栈中,则resultCode 为0(Activity.RESULT_CANCELED)。

如果Activity1的launchMode是standard,Activity2的launchMode是singleTask,我得到的resultCode是0。

堆栈发生了什么?

如果Activity1的launchMode为singleTop且Activity1使用startActivityForResult()方法启动自己,栈中有Activity1的两个实例。

堆栈发生了什么?

【问题讨论】:

    标签: android launchmode


    【解决方案1】:

    我自己得到了答案。

    对于第一个问题,我在方法startActivityForResult(Intent,int,Bundle)的注释中找到了答案。

    enter image description here

    如下图所示,如果ActivityY结束,显示的Activity可能是ActivityX,所以对于不确定的结果,它会立即收到取消结果。只有在api低于20时才会发生。

    enter image description here

    对于下一个问题,我在源码中找到了答案。

    ActivityStack topStack = getFocusedStack(); ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop); if (top != null && r.resultTo == null) { Slog.d("DDY", "========------ " ); if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) { if (top.app != null && top.app.thread != null) { if ((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) { ...... top.deliverNewIntentLocked(callingUid, r.intent);

    当一个Activity尝试自己启动时,r.resultTo不为null,所以它以标准模式启动。

    【讨论】:

      猜你喜欢
      • 2021-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-08
      • 2016-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多