Android 打算开始活动 - 但不要放在前面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10008879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:09:50  来源:igfitidea点击:

Intent to start activity - but don't bring to front

androidandroid-intentflags

提问by pulancheck1988

Description:

描述:

  • Activity Ais visible (or in the background)
  • Intent Iis received by a broadcast with valuable extras and then passes the extras to a new Intent I2that will be used to start activity A.
  • Outcome: Do not bringactivity to front ifactivity is in background.
  • 活动A可见(或在后台)
  • Intent I被带有有价值的额外内容的广播接收,然后将额外内容传递给一个新的 Intent I2,该Intent I2将用于启动活动A
  • 结果:如果活动在后台,不要将活动放在前面。

Code:

代码:

Intent I2= new Intent(context, MyActivity.class); 
I2.putExtra(..
I2.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK); // | Intent.FLAG_ACTIVITY_NEW_TASK); 
context.startActivity(I2);

Note: I added no "android:taskAffinity" to manifest.. i thought you should know

注意:我没有添加 "android:taskAffinity" 到 manifest.. 我想你应该知道

回答by vipin

if you want your activity to be in background add this line in the oncreate of activity

如果您希望您的活动处于后台,请在活动的 oncreate 中添加此行

moveTaskToBack(true);

回答by Anand M Joseph

You can use this line in your onCreate()method:

您可以在您的onCreate()方法中使用这一行:

moveTaskToBack(true);

回答by David Wasser

You don't want to start an Activityin the background. There are better ways to do what you want. You can have your Activityregister to receive the broadcast Intent, for example. It will get the call to onReceive()even if it is in the background. You can determine if your Activityis in the background by setting a variable to truein onPause()and to falsein onResume(). Then in onReceive(), if the variable is trueyou are in the background.

您不想Activity在后台启动。有更好的方法来做你想做的事。例如,您可以让您的Activity寄存器接收广播IntentonReceive()即使它在后台,它也会得到调用。您可以Activity通过将变量设置为trueinonPause()falsein来确定您是否在后台onResume()。然后在onReceive(),如果变量是true你在后台。