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

╄→гoц情女王★ 提交于 2020-03-17 06:44:04

问题


Description:

  • Activity A is visible (or in the background)
  • Intent I is received by a broadcast with valuable extras and then passes the extras to a new Intent I2 that will be used to start activity A.
  • Outcome: Do not bring activity to front if activity is in background.

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


回答1:


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

moveTaskToBack(true);



回答2:


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

moveTaskToBack(true);



回答3:


You don't want to start an Activity in the background. There are better ways to do what you want. You can have your Activity register 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 Activity is in the background by setting a variable to true in onPause() and to false in onResume(). Then in onReceive(), if the variable is true you are in the background.



来源:https://stackoverflow.com/questions/10008879/intent-to-start-activity-but-dont-bring-to-front

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!