2

I have a Service (called AlarmService) that opens an Dialog-themed Activity when intent is received (service is called at a specific time).

@Override
protected void onHandleIntent(Intent intent) {
    Intent dialogIntent = new Intent(ctx, LogoutConfirmDialog.class);
    dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(dialogIntent);
}

I have LogoutConfirmDialog defined in AndroidManifest.xml:

<activity android:name=".LogoutConfirmDialog"
            android:theme="@android:style/Theme.Holo.Dialog" />

Activity LogoutConfirmDialog just creates an AlertDialog and shows it:

public class LogoutConfirmDialog extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AlertDialog.Builder b = new AlertDialog.Builder(this);

        b.setTitle(R.string.dialog_logout_title);
        b.setMessage(R.string.dialog_logout_message);
        b.setPositiveButton(R.string.dialog_logout_positive, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    AlarmService.logout();
                }
            });
        b.setNegativeButton(R.string.dialog_logout_negative, null);
        AlertDialog dialog = b.create();
        dialog.show();
    }
}

Now the problem is that when the LogoutConfirmDialog is shown, it shows two dialogs on top of each other.

  1. First one is my dialog that has the title, message, positive and negative buttons set in onCreate of LogoutConfirmDialog.
  2. Second one having nothing but a title (some kind of default Dialog),

From the logs I can see that it LogoutConfirmDialog is started only once but window manager adds two windows.

ActivityManager﹕ START u0 {flg=0x10000000 cmp=com.mypackage/.LogoutConfirmDialog} from uid 10104 on display 0

WindowManager﹕ Adding window Window{d342658 u0 com.mypackage/com.mypackage.LogoutConfirmDialog} at 6 of 13 (after Window{7e66f7b u0 com.mypackage/com.mypackage.MainActivity})
V/WindowManager﹕ Adding window Window{1100c096 u0 com.mypackage/com.mypackage.LogoutConfirmDialog} at 6 of 14 (before Window{d342658 u0 com.mypackage/com.mypackage.LogoutConfirmDialog

How can I get rid of the first 'titleonly' -dialog? Or how to prevent it from adding this dialog?

Thanks in advance!

Edit I added two printscreens of the Dialogs to clarify the situation:

First Dialog (this is what I want) https://i.stack.imgur.com/mxTT0.png

The second Dialog shows up when either of the buttons is clicked, or it's dismissed: https://i.stack.imgur.com/bmCk7.png

RichieRock
  • 1,138
  • 1
  • 8
  • 13

3 Answers3

1

You call create and show. You can just make your builder to only call show, it automatically create with your predefined parameter. instead of this :

AlertDialog dialog = b.create();
dialog.show();

Show it with this : b.show();

  • Thank you for your answer. Unfortunately, this does not solve the problem. There still is another Dialog under the Dialog I want... – RichieRock Apr 13 '15 at 09:47
1

I have found a workaround. Though I am still baffled why the activity starts two Dialogs.

Solution:

Declare the Activity with tag android:theme="@android:style/Theme.NoDisplay" so that the Activity and hence default dialog (shown here) doesn't show up at all.

Then, in onCreate of LoginConfirmDialog I set the theme back to Holo.Dialog:

super.setTheme(android.R.style.Theme_Holo_Dialog);

Now I can show the new Dialog simply by calling builder.show(); and it doesn't show the weird 'title-dialog' that was confusing me.

So this workaround works but doesn't quite explain why this happened in the first place.

RichieRock
  • 1,138
  • 1
  • 8
  • 13
0

Do the following and try, instead of creating another object use the same object:

AlertDialog.Builder b = new AlertDialog.Builder(this);

        b.setTitle(R.string.dialog_logout_title);
        b.setMessage(R.string.dialog_logout_message);
        b.setPositiveButton(R.string.dialog_logout_positive, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    AlarmService.logout();
                }
            });
        b.setNegativeButton(R.string.dialog_logout_negative, null);
       b.show();

Hope this helps you.

keshav kowshik
  • 2,354
  • 4
  • 22
  • 45
  • Thanks for your answer but neither does this solve the problem. I could add few print screens of the issue where there are these two Dialogs if that helps.. ? – RichieRock Apr 13 '15 at 09:48
  • I have used this in lot of places, I have never faced this issue. Is there any other part of your code using alert dialog in same activity?, yes try using log statements and check from where another dialog getting opened. – keshav kowshik Apr 13 '15 at 10:00
  • This Dialog Activity is not called anywhere else in this code. I created this Activity only for this purpose when searching for a way to show AlertDialog from a service. (ref: [link](http://stackoverflow.com/a/22627480/3725740)) Do the screenshots I uploaded ring any bells? – RichieRock Apr 13 '15 at 10:11