Android:恢复singleTask活动

时间:2014-09-23 16:21:13

标签: android android-intent android-activity launchmode

我在端口上有一个UDP套接字的活动。如果按主页按钮,活动将在后台运行,则会调用 OnPause() OnStop()方法。现在我想收到一些UDP数据包后恢复活动。 阅读我理解的其他帖子:

  1. 将活动声明为android:launchMode="singleTask"(或singleInstance
  2. 然后,当我想恢复活动时:
  3. Intent intent = new Intent(this.getApplicationContext(), myActivity.class);
    
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    
    startActivity(intent);
    

    此解决方案不适用于我。对startActivity(intent)的调用未显示我在前台的活动,并且未调用onResume()

    以下标志执行此任务但我不想清除该任务并重新启动新任务。

    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    

1 个答案:

答案 0 :(得分:0)

为了将您自己的任务放到前台,您需要在非活动上下文上调用startActivity(),例如:

getApplicationContext().startActivity(intent);

此外,您无需使用任何特殊启动模式(singleTasksingleInstance)即可使用此功能。

这是在Android 4.4中修复的Android错误。由于这个问题来自2014年,我将假设OP正在看到这个问题。

请参阅How to resume Android Activity programmatically from background和评论主题。