I am developing an app which runs has a basic UI and service which can receive emergencies'. If the user is in the UI and presses home or Back a notification appears and the service continues to run. If an emergency is received by the service I want to open the Activity into the UI. To do this I have to use startActivity in the service with the Intent flag FLAG_ACTIVITY_NEW_TASK.
But this means that, if the UI is still running in the background, it doesn't get re-opened. Many of my UI interactions from that point onward seem to happen on the old, hidden UI not the new UI which is open. Also If i close the current UI I sometimes have the old UI behind it.
I would like that from a service I can either bring the old ui to the front OR open a new one, depending whether or not the old UI is still running.
I have tried using FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK together to both clear any other instances of the UI then open the UI in a new task but CLEAR_TASK is API 11 and over; my app supports 10.
Any help on the above is appreciated.
You can try to choose the launch mode "singleTask" in your manifest file :
With the "singleTask" launch mode, your activity will be instanciated only one time.
What I've done with this is do a
bindService
in the Activity'sonResume
(so you get theService
instance) and then add a method to theService
instance that receives aHandler
instance from theActivity
. InonPause
you set theHandler
on theService
to null. This way theService
knows (by checking its internalHandler
instance) if there is already a UI available. If there is, it sends theHandler
aMessage
that things happened. If there is noHandler
, it launches a new UI.