2

I wrote an application which has a flow in which there is a waiting for some server event to happen (waiting by polling till desired resault returned). it is natural that the wait time will be minutes. I would like that the application users will be able to run other applicatins, but once my applications thread got the desired server response, I want my application to return to the foreground.

How do I return my application to the forground (from a thread that my application created)?

I tried the common way of runOnUiThread, but it didn't work while my application is not on the foreground.

thanks.

Andro id
  • 31
  • 1
  • 3

2 Answers2

6

It's a bad thing to force your application upon users, perhaps they are doing smth more important?

The way to solve this is to use a notification, then your users will be notified that new data have arrived, and can select to open your application if and when they got the time.

H9kDroid
  • 1,814
  • 15
  • 17
  • thanks. when the user runs the application, he is aware that its a long asynchronious action, and it should popup and take over when its ready to. – Andro id Nov 13 '11 at 12:12
1

In the spirit of answering the question posed... see this post for ways that you might achieve this.

However, just because you can doesn't mean you should! Your users may have thought they'd be willing to go immediately back into your app, when they started the async task, but that was before their friend got run over by a bus, and they now want to use the phone to call an ambulance...

So, no, it should absolutely not popup and take over. It should display a notification to the user, who will click on it when they're good and ready. As the android docs themselves say: "A background service should never launch an activity on its own in order to receive user interaction." (emphasis theirs!)

Community
  • 1
  • 1
Teasel
  • 960
  • 6
  • 6
  • thanks! your answer helped me. Intent.FLAG_ACTIVITY_REORDER_TO_FRONT, Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY and Intent.FLAG_ACTIVITY_NEW_TASK resolved it. (maybe I can eliminate one flag. have to check it). I also got impressed not only from the android knowlage. also from the friend and the bus examplple.... – Andro id Nov 14 '11 at 10:02
  • this only works for me if my app was the latest app in the foreground; i.e. when I'm on the homescreen. If I'm on the homescreen coming from another app it will put that app to front!?! Managed to solve it in combination with http://stackoverflow.com/questions/6919616/android-how-to-bring-a-task-to-the-foreground (moveTaskToFront). Not intending to use this in a finished app, but for testing we need some kind of remote control, so using a web socket I trigger some actions – Matthias Mar 26 '15 at 11:41