One of my Activities is launched from Service with Intent.FLAG_ACTIVITY_NEW_TASK. When I press back button and start this Activity
it opens and when I press Back Button I see the regular Android home. But if I press Home Button and then start my Activity
from Service
and press Back I see the previous Activity from my App. But I'd like to see the regular Android home.
Could anyone help me please?
If I understand you correctly, you need to have a single instance of that Activity
. The way I see it you have 2 options:
launchMode
to singleInstance
in your application manifest. singleInstance
is not recommended for most apps (The link I have provided you doesn't detail too much why, but that is a different story). Or if you have multiple activities, then having an activity with this launch mode may not be appropriate. You need to handle the case when you have your activity on stack or you don't have it. From service you can send an ordered broadcast that your activity can process and abort if it's on stack (in your activity #onCreate() and #onDestroy() you will register, resp un-register that receiver. Remember to register with a high ordinal). More on ordered broadcast receivers here . So if the activity is on the stack, you will receive that intent and drop the broadcast. If the activity is not on the stack then another broadcast receiver with a lower priority will receive that intent - in this case, your service. So, again: onCreate and onDestroy - register resp. unregister the broadcast receiver. In its onReceive()
method you will handle what you're doing now - open the Activity with FLAG_ACTIVITY_NEW_TASK
. Hope it makes sense. Succeeded to do it with the help of:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.