Started activity from home key screen

陌路散爱 提交于 2019-12-09 18:49:39

问题


I have a background Service which starts an activity,

Intent i = new Intent(this, MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);

After destroying this Activity and restart it over the "long press home key menu", this Activity starts again. But I want to start the main activity instead. How could I realise this?


回答1:


Could you explain in more detail? If I understand your problem try setting the FLAG_ACTIVITY_NO_HISTORY.

Alternatively a manual solution would be to check the FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY on the intent in MyActivity and launch to the main activity if you see this flag set. The following code should do that:

if ((getIntent().getFlags() & FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) > 0) {
   activity.startActivity(new Intent(context , MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));    
}



回答2:


The problem is like you started the activity from service-->Notification came-->user launch the app agian-->No notification-->main activity came on foreground Now if the application is started from the "long press home key menu" main activity is starting and showing the Notification.

so one Clear resolution is make Main activity as "Exclude From recent = true" and "No History = true;" user will not be able to see your activity in the "long press home key menu"



来源:https://stackoverflow.com/questions/5102052/started-activity-from-home-key-screen

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