9

I need a popup dialog to be shown when i get a message from different thread but the dialog should be not dependent on Activity i.e, it should display the dialog wherever the screen focus is.

Can it be done? Because the dialog is handled per Activity, I thought of using a service but again it would be one more thread added and I want to avoid that.

Any other options available?

Janusz
  • 187,060
  • 113
  • 301
  • 369
Sam97305421562
  • 3,027
  • 10
  • 35
  • 45
  • I believe he's trying to ask this: How can you start a dialog from a service that is running in a separate thread? There may be any number of activities running. To show a dialog, you need to specify the current activity. – Edward Anderson Oct 13 '10 at 23:24
  • A possible solution is to use an activity: http://stackoverflow.com/posts/3912748/revisions –  Jan 29 '13 at 08:10
  • https://stackoverflow.com/a/29804684/2149195 – RBK Sep 12 '17 at 13:36

3 Answers3

17

If you're trying to ask how to show a dialog when your activity is not the focused activity on the user's phone then try using Notifications instead. Popping up a dialog over a different application interrupts the user when they may be doing something else. From the Android UI guidelines:

Use the notification system — don't use dialog boxes in place of notifications

If your background service needs to notify a user, use the standard notification system — don't use a dialog or toast to notify them. A dialog or toast would immediately take focus and interrupt the user, taking focus away from what they were doing: the user could be in the middle of typing text the moment the dialog appears and could accidentally act on the dialog. Users are used to dealing with notifications and can pull down the notification shade at their convenience to respond to your message.

A guide to create notifications is here: http://developer.android.com/guide/topics/ui/notifiers/notifications.html

Intrications
  • 16,782
  • 9
  • 50
  • 50
  • hi, As i phone have Application delegate which always listen to the to the UI changes is there any thing similiar in Android ? – Sam97305421562 Jun 23 '09 at 05:12
  • Facebook Messenger (Bubbles) doesn't follow this rule . . . – sospedra Jul 19 '14 at 19:57
  • @Bursos I'm not sure what your point is. Facebook often doesn't follow Android UI guidelines. Also the bubbles don't take over the user input like a Dialog would. – Intrications Jul 20 '14 at 11:10
  • @Intrications Maybe I'm wrong, but I think those Facebook bubbles doesn't respect the notifications-dialogs pattern because when somebody talk to you it shall be notificated and not poped-up with a dialog (confirming if you want to check the conversation or not). I only was trying to point that a big corporation like Facebook doesn't follow always the main Android's rules. – sospedra Jul 20 '14 at 14:04
1

if I understand you correctly you could use a base class for all of your activities

public abstract class BaseActivity extends Activity{
    protected static BaseActivity current_context = null;

    @override
    protected void onPause(){
        current_context = null;
        super.onPause();
    }

    @override
    protected void onResume(){
        current_context = this;
        super.onResume();
    }

    public static void showDialog(/*your parameters*/){
        //show nothing, if no activity has focus
        if(current_context == null)return;
        current_context.runOnUiThread(new Runnable() {
            @override
            public void run(){
                AlertDialog.Builder builder = 
                    new AlertDialog.Builder(current_context);
                //your dialog initialization
                builder.show();
            }
        });
    }
}

in your thread show your dialog with BaseActivity.showDialog(..) But this approach doesn't work if you want to show your dialog on top of any activity of the target device.

mini
  • 11
  • 2
1

Alternative solution :

AlertDialog dialog;
//add this to your code
    dialog = builder.create();
    Window window = dialog.getWindow(); 
    WindowManager.LayoutParams lp = window.getAttributes();
    lp.token = mInputView.getWindowToken();
    lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
    window.setAttributes(lp);
    window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
//end addons
alert.show();
Dori
  • 915
  • 1
  • 12
  • 20
Maher Abuthraa
  • 17,493
  • 11
  • 81
  • 103
  • 1
    So... where do you think I could get a mInputView in a background thread? ;) – Janusz Jan 24 '11 at 10:54
  • (new AlertDialog.Builder(myAppContext)).create() gives me java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare(). – Jeno Csupor Mar 03 '11 at 22:33
  • you shall use the activity context and not the application context. – 7heViking May 31 '12 at 13:53
  • What does this line depict? `lp.token = mInputView.getWindowToken();` We need a view to get the token? If yes, we need an activity context! – C-- Jan 01 '14 at 06:28