[英]Android: resume a singleTask activity
I have an activity with an UDP socket on a port. 我在端口上有一个UDP套接字的活动。 If I press the Home button the activity goes in background, OnPause() and OnStop() methods are called. 如果按“主页”按钮,活动将在后台进行,将调用OnPause()和OnStop()方法。 Now I want to resume my activity when I receive some UDP packet. 现在,我想在收到一些UDP数据包后恢复活动。 Reading the other posts I understand I have to: 阅读其他我理解的帖子,我必须:
android:launchMode="singleTask"
(or singleInstance
) 将活动声明为android:launchMode="singleTask"
(或singleInstance
) 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);
This solution does not work form me. 这个解决方案对我不起作用。 The call to startActivity(intent)
does not show my activity on foreground and onResume()
is not called. 调用startActivity(intent)
不会在前台显示我的活动,也不会调用onResume()
。
The following flags do the job but I don't want to clear the task and restart a new one. 以下标志可以完成这项工作,但我不想清除任务并重新启动新任务。
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
In order to bring your own task to the foreground, you need to call startActivity()
on a non-Activity Context, for example: 为了将您自己的任务放在最前面,您需要在非活动上下文上调用startActivity()
,例如:
getApplicationContext().startActivity(intent);
Also, you don't need to use any special launch mode ( singleTask
or singleInstance
) for this to work. 另外,您无需使用任何特殊的启动模式( singleTask
或singleInstance
)即可运行。
This is an Android bug that was fixed in Android 4.4. 这是Android 4.4中修复的一个Android错误。 Since this question is from 2014, I will assume that OP was seeing this problem. 由于这个问题来自2014年,因此我假设OP遇到了这个问题。
See How to resume Android Activity programmatically from background and the comment thread. 请参阅如何通过后台和注释线程以编程方式恢复Android Activity 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.