Ad

AlertDialogs Show/dismiss One After Each Other

I have a list of messages and times that I want to show them with AlertDialog; alertDialog should dismiss after the time has passed and the next dialog should be shown to the end of the list

AlertList model structure: (int time, String message, boolean cancelable)

AlertList a0 = new AlertList(5, "11111111111", false);
AlertList a1 = new AlertList(2, "222222222", true);
AlertList a2 = new AlertList(2, "3333333333333", false);
AlertList a3 = new AlertList(2, "4444444444444444444444", true);

List<AlertList> list = new ArrayList<>();
list.add(a0);
list.add(a1);
list.add(a2);
list.add(a3);

I want show alertDialog after finish another alertDialog

        if (list != null && list.size() > 0) {

            for (int i = 0; i < list.size(); i++) {
//                synchronized (this) {

                int finalI = i;
                AlertDialog dialog = new AlertDialog.Builder(context).create();
                new Handler().post(() -> {
                    dialog.setMessage(list.get(finalI).getStrComment());
                    dialog.setCancelable(false);
                    dialog.setCanceledOnTouchOutside(false);
                    if (list.get(finalI).isCancelable()) {
                        dialog.setCancelable(true);
                        dialog.setCanceledOnTouchOutside(true);
//                        } else {
                    }

                    dialog.show();

                    new Handler().postDelayed(() -> {
                        dialog.dismiss();
//                            resume();
                    }, list.get(finalI).getTime() * 1000);
                    dialog.setOnDismissListener(dialog1 -> notify());
                });
                try {
                    Thread.sleep(list.get(finalI).getTime() * 1000 + 500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
//                    if (!paused)
//                        pause(finalI);
//                    notify();
//                }
        }
    }
}
Ad

Answer

You should not use any loop (for-loop) in this case, because it create all the dialog synchronously, instead you have to open the next dialog when you notified the previous dialog (or action) has been done.

In this scenario you can get notified either by setOnDismissListener or when the user take an action like clicking on a button, here is where recursive function calling could help you to accomplish what you want to do

private void showDialogs(List<AlertList> list) {
    if (list == null || list.size() == 0) return;

    AlertList data = list.get(0);
    list.remove(0);

    AlertDialog dialog = new AlertDialog.Builder(context)
            .setMessage(data.getStrComment())
            .setCancelable(data.isCancelable())
            .create();
    dialog.setCanceledOnTouchOutside(data.isCancelable());

    CountDownTimer timer = new CountDownTimer(data.getTime() * 1000, data.getTime() * 1000) {

        @Override
        public void onTick(long millisUntilFinished) { }

        @Override
        public void onFinish() {
            dialog.dismiss(); 
        }
    };

    dialog.setOnDismissListener(dialog1 -> {
        timer.cancel();
        showDialogs(list);
    });

    dialog.show();
    timer.start();
}

And then you should call showDialogs method once in the onCreate method for instance.

Ad
source: stackoverflow.com
Ad