5

I want to launch a dialog from a service that hovers over whatever the user is currently looking at. The dialog gets launched like this: service gets trigger to open dialog > start transparent activity > transparent activity shows dialog.

My problem is when the user opens the app, launches into the main menu, and then presses HOME to leave. By pressing HOME, it leaves the main menu activity on pause, not destroyed, and when the service starts the dialog, the main menu gets shown underneath the transparent activity; causing the dialog to loose the affect of hovering over whatever the user is looking at.

How can make it so that the transparent activity gets opened independently of any other activities in the app? The only way to prevent this currently is to finish all the activities when they are paused; but this is impractical.

Brian
  • 7,457
  • 15
  • 61
  • 103
  • Possible duplicate of [Alert dialog from Android service](http://stackoverflow.com/questions/3599563/alert-dialog-from-android-service) – araks Nov 06 '15 at 17:36
  • https://stackoverflow.com/a/29804684/2149195 – RBK Sep 12 '17 at 13:35

3 Answers3

13

This is the last thing we want. :-)

1. Dialog boxes from Services

One of the best experiences in mobile devices, IMHO, and Android in particular, is that after decades, we got rid of system-wide, pesky alert dialogs. Finally, best practices [1, 2] for user interaction gave us a way to avoid the infamous disseminate use of MessageBox(hwnd, lpText, lpCaption, uType), competing for focus and for the attention of the poor user. See video parody above.

The reason it feels awkward to start a dialog from a Service is exactly because it is supposed to be a background task, without user interaction. By concept, you shouldn't be doing this. That's the reason why we see these tricks (transparent activities, what a silly thing) to cheat the design guidelines in the first place. They are bad, they disrupt the user experience, they steal focus and attention. They disrupt our work.

2. Use notifications instead

Whenever you want to notify a user of something from the background, when the user is somewhere else, you use a notification. It's the default pattern, and it doesn't bother the user.

Therefore, you should be sending notifications from your Service.

From there, if the user is interested, then he will touch the notification and you start your own activity, possibly resuming your activity, creating a new one, and then using a dialog requesting action to be performed, or whatever you want to do.

3. Finally, do NOT use FLAG_ACTIVITY_MULTIPLE_TASK

You should not, ever, use this flag unless you have carefully read and fully understood the documentation, and the implications of using that flag.

Do not use this flag unless you are implementing your own top-level application launcher. (...) Because the default system does not include graphical task management, you should not use this flag unless you provide some way for a user to return back to the tasks you have launched.

Really. In this case, just don't.

Community
  • 1
  • 1
davidcesarino
  • 15,491
  • 15
  • 66
  • 108
  • Thanks David for your insight, your first and second points are good and I agree with them, but the situation I described in my question is not actually my real situation. I just cut it down to what I needed to convey; the service does just show dialogs out of nothing, it happens upon user request. I read and understood to the best of my knowledge what `FLAG_ACTIVITY_MULTIPLE_TASK` does and I've implemented the conditions that the documentation says. I've got it all under control, thanks! – Brian Dec 02 '11 at 05:25
  • That's fine. Anyway, I also answered for the Googlers, since your answer is indeed inappropriate to those that don't understand it. It seems this flag is the answer when you just acknowledged that there are other implications on the real scenario as well. Thus, my remark. – davidcesarino Dec 02 '11 at 16:24
  • 1
    About the flag itself, basically you need to provide a way for the user to navigate back to the activity, which means you're either creating your own launcher or you are implementing low level activity/task trackers that you can use later on in order to bring them to the foreground again (in rare circumstances, you don't: dialers). The implications are there when the user presses the home button without following your code logic as you thought. However, if you say you understood, then it's fine to me, as you are the best person to evaluate this need. Good luck! – davidcesarino Dec 02 '11 at 16:27
  • Thanks so much for this clarification! I'm glad you some time to fully explain that to me. – Brian Dec 03 '11 at 08:22
  • 1
    @XO. As a personal _opinion_, I dislike chat heads for exactly this reason, even more now that we have direct replies and notification bundling (besides the _fact_ it can, and has in the past, lead to apps fighting for attention, as comically presented above). But I'd agree this also goes into the territory of _opinions_, saying “your mileage may vary”, as I can understand why someone would want their contacts to keep appearing in front of anything else they might be doing with their phones. It depends on your priorities and how you want to interact with them. Still, not my cup of tea. – davidcesarino Mar 20 '17 at 19:01
  • Amazing insight. Thanks for sharing this. – Tiago Dávila Oct 27 '17 at 10:27
2

You may consider using alertDialog with TYPE_SYSTEM_ALERT instead of activity:

AlertDialog alertDialog = new AlertDialog.Builder(this)
                    .setTitle("Title")
                    .setMessage("Are you sure?")
                    .create();

alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.show();

Please note that you have to use the following permission:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
Eliran Kuta
  • 3,580
  • 3
  • 22
  • 28
1

Found this out myself, just add the Intent.FLAG_ACTIVITY_MULTIPLE_TASK flag to the launch Intent; of course in conjunction with the Intent.FLAG_ACTIVITY_NEW_TASK flag.

Brian
  • 7,457
  • 15
  • 61
  • 103
  • 1
    As David points out below, this flag should NOT be used for this purpose: http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_MULTIPLE_TASK – Thom Seddon Apr 24 '13 at 12:44