【问题标题】:NavUtils.navigateUpTo() does not start any ActivityNavUtils.navigateUpTo() 不启动任何活动
【发布时间】:2013-11-28 18:14:25
【问题描述】:

我有两个活动

  • MainActivity
  • DeepLinkActivity

我设置了一切以使用NavUtils 进行导航,如建议的here、here 和here。

我想要实现的是:

  1. 通过深层链接启动DeepLinkActivity
  2. 向上推
  3. 转到MainActivity

只要最近的应用程序中有我的应用程序的任何任务,一切都运行良好。

但是,当我从最近的应用中滑动我的应用时,它的行为如下:

  1. 从最近的应用中滑动我的应用
  2. 通过深层链接启动DeepLinkActivity
  3. 向上推
  4. 我的应用程序关闭,就像按下返回键时一样

我调试了代码,发现NavUtils.shouldUpRecreateTask() 返回false。 upIntent 将一切设置为正常,就像我的 Component 设置一样。 但是,NavUtils.navigateUpTo() 的行为就像对 finish() 的调用一样。 没有日志语句,什么都没有。

任何想法,如何解决?

AndroidManifest.xml

<activity
    android:name=".DeepLinkActivity"
    android:parentActivityName="my.package.MainActivity">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="my.package.MainActivity"/>
    <intent-filter>
        <!-- Some intent filter -->
    </intent-filter>
</activity>

DeepLinkActivity.java

@Override
public boolean onOptionsItemSelected(final MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            Intent upIntent = NavUtils.getParentActivityIntent(this);
            if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
                // create new task
                TaskStackBuilder.create(this).addNextIntentWithParentStack(upIntent)
                        .startActivities();
            } else {
                // Stay in same task
                NavUtils.navigateUpTo(this, upIntent);
            }
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

----- 编辑-----

我意识到一些 Google Apps 以同样的方式损坏。如果你跳例如从搜索到联系人,在 AB 中按向上,您会发现自己在主屏幕上,而不是联系人应用程序。 (API19/cm11)

【问题讨论】:

  • 这条线是否执行过??返回真;
  • 好吧,Android Studio 中的调试器从NavUtils.navigateUpTo(this, upIntent); 跳转到return super.onOptionsItemSelected(item);,但我猜/希望,这是一个错误。无论如何,Intent 应该在NavUtils.navigateUpTo(this, upIntent); 启动
  • Android studio 甚至告诉我return true 的行没有可执行代码。 o.O
  • 你是如何开始 DeepLinkActivity 的?可以发一些代码吗?
  • 我有一些意图过滤器,并通过从浏览器或邮件应用程序启动 URL 来启动。

标签: android android-activity android-lifecycle


【解决方案1】:

我对 OPs 问题的解决方案:

public void navigateUp() {
    final Intent upIntent = NavUtils.getParentActivityIntent(this);
    if (NavUtils.shouldUpRecreateTask(this, upIntent) || isTaskRoot()) {
        Log.v(logTag, "Recreate back stack");
        TaskStackBuilder.create(this).addNextIntentWithParentStack(upIntent).startActivities();
    } else {
        NavUtils.navigateUpTo(this, upIntent);
    }
}
如果 DeepLinkActivity 是任务的根目录(应用程序的初始启动或应用程序先前已通过任务管理器终止),则

isTaskRoot() 将返回 true。这样,当应用程序任务已经在前台时,如果通过链接启动活动,我就不会丢失现有的后台堆栈。

【讨论】:

  • 其实这是@Mikooos 解决方案。
  • 要防止出现空指针,请检查 upIntent 是否为空。但答案就像一个先生:)
【解决方案2】:

我认为这个方法有问题。我已经阅读了支持库源代码,并且该方法检查了意图的操作。它仅在您的应用程序之前创建时才有效..正如您所描述的,如果您从应用程序预览中杀死它,shouldUp 方法将停止工作。

我已经使用我自己的“shouldUpRecreateTask”解决了这个问题。当我收到一个直接创建 Activity 的通知(就像你的行为一样)时,我会从我的 BroadCastReceiver 发送一个意图内的自定义操作。然后,在我的方法中,我做下一件事:

private final boolean shouldUpRecreateTask(Activity from){
    String action = from.getIntent().getAction();
    return action != null && action.equals(com.xxxxxx.activities.Intent.FROM_NOTIFICATION);
}

......................

 if (shouldUpRecreateTask(this)) {
      TaskStackBuilder.create(this)
       .addNextIntentWithParentStack(upIntent)
       .startActivities();
 } else {
       fillUpIntentWithExtras(upIntent);
       NavUtils.navigateUpTo(this, upIntent);
 }

【讨论】:

  • 这基本上是我用自己的答案找到的解决方案,但有更好的解释。感谢您的洞察力。
  • 您可以使用 Activity#isTaskRoot 方法而不是添加自定义操作。我的向上导航看起来像这样,我没有发现问题:pastebin.com/dTz1UEH5 Intent upIntent = NavUtils.getParentActivityIntent(this); if (NavUtils.shouldUpRecreateTask(this, upIntent) || isTaskRoot()) { TaskStackBuilder.create(this) .addNextIntentWithParentStack(upIntent) .startActivities(); } else { NavUtils.navigateUpTo(this, upIntent); }
  • 发送自定义操作或检查 isTaskRoot() 是否会重新创建父活动,但在我的情况下,当新通知到达时,点击通知将不再启动活动。它什么都不做。
【解决方案3】:

看起来NavUtils.shouldUpRecreateTask(this, upIntent) 没有为这种特殊情况做好准备。

我目前的解决方法是检查 Activity 是否被深度链接,并针对此类情况强制使用新的任务堆栈。

就我而言,我在内部传递EXTRAs 中的对象。 但是,如果 Activity 是从外部通过指向特定 URL 的链接或通过触摸 NFC 感知设备启动的,则会设置 ACTION 和 Uri。

@Override
public boolean onOptionsItemSelected(final MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            Intent upIntent = NavUtils.getParentActivityIntent(this);
            if (NavUtils.shouldUpRecreateTask(this, upIntent)
                    || getIntent().getAction() != null) { // deep linked: force new stack
                // create new task
                TaskStackBuilder.create(this).addNextIntentWithParentStack(upIntent)
                        .startActivities();
            } else {
                // Stay in same task
                NavUtils.navigateUpTo(this, upIntent);
            }
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

【讨论】:

  • 谢谢。我将通过为意图“is_deeplink”设置一个额外的布尔值并检查它以创建新任务来实现这个想法。
【解决方案4】:

您是否使用&lt;activity-alias&gt; 作为您的主/启动器?当我将&lt;activity-alias&gt; 更改为&lt;activity&gt; 时,向上导航可以正常工作。由于某些奇怪的原因,涉及别名时导航无法正常工作。

还有一个奇怪的 UI 故障,表明本机 ActivityManager 存在错误。使用navigateUpTo() 导航到应用程序的主要活动后,按设备的返回键。当前应用程序将执行活动退出动画,然后紧接着,下一个最近的应用程序也将执行活动退出动画。即使您从 Android 的主屏幕启动到当前应用程序,也会发生这种情况。如果您清除所有最近的应用程序并再次尝试导航向上然后返回步骤,退出动画期间将显示一个随机(即意外)应用程序。

在这些情况下,绝不应展示其他应用。似乎活动管理器没有正确管理历史堆栈。

可以在this diff底部出现的以下方法中找到违规错误:

+    public boolean navigateUpTo(IBinder token, Intent destIntent, int resultCode,
+            Intent resultData) {
+        ComponentName dest = destIntent.getComponent();
+
+        synchronized (this) {
+            ActivityRecord srec = ActivityRecord.forToken(token);
+            ArrayList<ActivityRecord> history = srec.stack.mHistory;
+            final int start = history.indexOf(srec);
+            if (start < 0) {
+                // Current activity is not in history stack; do nothing.
+                return false;
+            }
+            int finishTo = start - 1;
+            ActivityRecord parent = null;
+            boolean foundParentInTask = false;
+            if (dest != null) {
+                TaskRecord tr = srec.task;
+                for (int i = start - 1; i >= 0; i--) {
+                    ActivityRecord r = history.get(i);
+                    if (tr != r.task) {
+                        // Couldn't find parent in the same task; stop at the one above this.
+                        // (Root of current task; in-app "home" behavior)
+                        // Always at least finish the current activity.
+                        finishTo = Math.min(start - 1, i + 1);
+                        parent = history.get(finishTo);
+                        break;
+                    } else if (r.info.packageName.equals(dest.getPackageName()) &&
+                            r.info.name.equals(dest.getClassName())) {
+                        finishTo = i;
+                        parent = r;
+                        foundParentInTask = true;
+                        break;
+                    }
+                }
+            }
+
+            if (mController != null) {
+                ActivityRecord next = mMainStack.topRunningActivityLocked(token, 0);
+                if (next != null) {
+                    // ask watcher if this is allowed
+                    boolean resumeOK = true;
+                    try {
+                        resumeOK = mController.activityResuming(next.packageName);
+                    } catch (RemoteException e) {
+                        mController = null;
+                    }
+
+                    if (!resumeOK) {
+                        return false;
+                    }
+                }
+            }
+            final long origId = Binder.clearCallingIdentity();
+            for (int i = start; i > finishTo; i--) {
+                ActivityRecord r = history.get(i);
+                mMainStack.requestFinishActivityLocked(r.appToken, resultCode, resultData,
+                        "navigate-up");
+                // Only return the supplied result for the first activity finished
+                resultCode = Activity.RESULT_CANCELED;
+                resultData = null;
+            }
+
+            if (parent != null && foundParentInTask) {
+                final int parentLaunchMode = parent.info.launchMode;
+                final int destIntentFlags = destIntent.getFlags();
+                if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
+                        parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
+                        parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
+                        (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
+                    parent.deliverNewIntentLocked(srec.app.uid, destIntent);
+                } else {
+                    try {
+                        ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
+                                destIntent.getComponent(), 0, UserId.getCallingUserId());
+                        int res = mMainStack.startActivityLocked(srec.app.thread, destIntent,
+                                null, aInfo, parent.appToken, null,
+                                0, -1, parent.launchedFromUid, 0, null, true, null);
+                        foundParentInTask = res == ActivityManager.START_SUCCESS;
+                    } catch (RemoteException e) {
+                        foundParentInTask = false;
+                    }
+                    mMainStack.requestFinishActivityLocked(parent.appToken, resultCode,
+                            resultData, "navigate-up");
+                }
+            }
+            Binder.restoreCallingIdentity(origId);
+            return foundParentInTask;
+        }
+    }

【讨论】:

    【解决方案5】:

    我发现以下内容对我有用。如果 Activity 与另一个 Activity 或通知有深度链接,则会在您向上导航时创建堆栈,否则,这些 Activity 只会被带到前面。

    case android.R.id.home:
    Intent upIntent = NavUtils.getParentActivityIntent(this);
    upIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    startActivity(upIntent);
    finish();
    return true;
    

    如果你有

    android:parentActivityName="parent.activity"
    

    在您的清单中

    【讨论】:

      【解决方案6】:

      这对我也不起作用。所以我是这样处理的:

         @Override
          public boolean onOptionsItemSelected(MenuItem item) {
              if (item.getItemId() == android.R.id.home) {
      
                      Intent intent = new Intent(this, MainActivity.class);
                      startActivity(intent);
                      finish();
                      return true;
      
              }
              return super.onOptionsItemSelected(item);
          }
      

      我的 MainActivity 在 android manifest 中被标记为 singleTask

      android:launchMode="singleTask"
      

      【讨论】:

        【解决方案7】:

        如果您从通知转到活动,则必须在创建通知时创建堆栈,请查看此答案:NavUtils.shouldUpRecreateTask fails on JellyBean

        【讨论】:

          【解决方案8】:

          试试这个:

          @Override
          public boolean onOptionsItemSelected(final MenuItem item) {
              switch (item.getItemId()) {
                  case android.R.id.home:
                      Intent upIntent = NavUtils.getParentActivityIntent(this);
                      if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
                          // create new task
                          TaskStackBuilder.create(this).addNextIntentWithParentStack(upIntent)
                                  .startActivities();
                      } else {
                          // Stay in same task
                          NavUtils.navigateUpTo(this, upIntent);
                      }
                      break;
                  default:
                      return super.onOptionsItemSelected(item);
              }
              return true;
          }
          

          【讨论】:

          • 从NavUtils.navigateUpTo(this, upIntent); 到return true;
          • if 中的条件变成了false,它跳转到else 块breaking the switch` 并在最后一行返回true。
          • 你期待它是真的吗?
          • 我不知道为什么,但是我的父活动丢失了他的变量,并且 savedInstanceState 为空。
          【解决方案9】:

          尝试以下对我有用的答案,无论是从通知启动活动还是从父活动启动活动。

          @Override
          public boolean onOptionsItemSelected(MenuItem item) {
              switch (item.getItemId()) {
                  case android.R.id.home:
                      Intent upIntent = NavUtils.getParentActivityIntent(this);
                      if (NavUtils.shouldUpRecreateTask(this, upIntent) || isTaskRoot()) {
                          TaskStackBuilder.create(this).addNextIntentWithParentStack(upIntent)
                                  .startActivities();
                      } else {
                          finish();
                      }
                      return true;
                  default:
                      return super.onOptionsItemSelected(item);
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2014-03-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-01-20
            • 1970-01-01
            相关资源
            最近更新 更多