Skip to content
Advertisement

The AlertDialog dosen’t show when activity start

This is my first time asking for help here.
Basically the problem is that I want to show an AlertDialog and then start an activity with an intent. The problem is that the intent starts without showing the AlertDialog and I don’t get why. But if I delete the intent code the alert shows up.

if (userImp == null) {
    AlertDialog alert = builder2.create();
    alert.show();
    LAUNCH_ACTIVITY = 0;
    Intent intent = new Intent(User.this, Credenziali.class);
    startActivity(intent);
}

Please anyone help me. Thanks.

Advertisement

Answer

AlertDialog will be related current activity.
So If current activity disappeared, AlertDialog will be hided also. To fix this problem, you need to show AlertDialog first, and then catch dismiss event.

Here is code snippet. so you can try this method.

if (userImp == null) {
    AlertDialog alert = builder2.create();
    alert.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialogInterface) {
           // your code after dissmiss dialog
           LAUNCH_ACTIVITY = 0;
           Intent intent = new Intent(User.this, Credenziali.class);
           startActivity(intent);
        }
    });
    alert.show();
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement