-1

I have this function where I need to return a list depending of what user pressed in Alert Dialog (cancel or save).

But I have an issue, let's imagine we have a list with a size of 10. Then on the iteration of that list it will build 10 alert dialogs at the same time plus a dark black shadow at the background caused by these.

So I'd like to "pause" until user pressed or find a way to don't pop up all these alert dialog at the same time and just appear one by one once pressed a button.

A quick reminder: I need to return a list after all dialogs have been pressed.

Question: How could I do that?

Barrufet
  • 495
  • 1
  • 11
  • 33
  • Why u creating dialog inside loop in first place . That's already seems messed up . – ADM Jan 10 '21 at 14:23
  • Because I evaluate first a list of items, then I set on a new list of items that will require user to confirm about what to do with that, so I loop that with alert dialogs waiting for user to tell me what to do with those items – Barrufet Jan 10 '21 at 14:30

2 Answers2

1

It would be better if you provided some code with this. Anyway, even though this is not something I would do and create 10 dialogs in the for loop, this can be done.

Just create a Boolean inside your for loop which will be used to check if the dialog is dismissed.

for(int i = 0; i < list.size(); ++i) {
        Boolean isDismissed = false;
        AlertDialog d = new AlertDialog(getBaseContext());
        d.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                 isDismissed = true;
            }
        });
        //start your dialog

        while(!isDismissed) {
            //do nothing
        }
}

As I said, I wouldn't do this.

Because I evaluate first a list of items, then I set on a new list of items that will require user to confirm about what to do with that, so I loop that with alert dialogs waiting for user to tell me what to do with those items

There is a much better way to do this. Why not starting one CustomDialog which will ask the user what to do with those items. He could choose options for each item with a spinner or if options are KEEP or DELETE just use checkbox or something.

SlothCoding
  • 1,546
  • 7
  • 14
  • Yeah this is the solution that I've thought but that seemed so bad, and the creation of alert dialogs in a loop is also a bad idea but I can't think of a current one that fits on my actual project. Anyways, thank you for the solution I'll let time pass and see if I ever find a better solution. – Barrufet Jan 10 '21 at 14:54
0

So as people said, creating Alert Dialogs in a loop is a bad practice so my solution into this is just setting a view on Fragment that acts like a Dialog but I just turn it visible and gone whenever I need. This seems a proper solution for my case.

When user accept or cancel the view (clicking on button) just send it to the viewmodel and the viewmodel will evaluate if there are still items on the list. If there are items then show again this "view" on Fragment asking to user what to do :)

I don't have code to show because I haven't done it yet but I have thought for a while and this is the best I can think about. Hope it helps for someone who is in the same situation!

Barrufet
  • 495
  • 1
  • 11
  • 33