简体   繁体   English

将应用程序带到后台然后到前台

[英]Bring application to background and then to foreground

What method should I use to move my app to background and then move it to foreground again? 我应该使用什么方法将我的应用程序移动到背景,然后再将其移动到前台? I tried using moveTaskToBack(true) and the activity is moved to background successfully but then I can't move it to foreground. 我尝试使用moveTaskToBack(true)并将活动成功移动到后台,但后来我无法将其移动到前台。 I tried starting the activity again using startActivity() but with no success and there seems to be no method moveTaskToFront() or something similar. 我尝试使用startActivity()再次启动活动,但没有成功,似乎没有方法moveTaskToFront()或类似的东西。

Use moveTaskToBack() to move your app to the background. 使用moveTaskToBack()将您的应用移动到后台。

To move it to the foreground, use the following code: 要将其移至前台,请使用以下代码:

Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName());
startActivity(intent);

If you are trying to do this from a Service or BroadcastReceiver then you will need to do this before calling startActivity() : 如果您尝试从ServiceBroadcastReceiver执行此操作,则需要在调用startActivity()之前执行此操作:

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

谢谢它通过添加以下意图为我工作

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM