6

I was testing the behavior of AlertDialog to integrate in a bigger component. I am unable to show the same dialog again. Here is the test code:

public class MainActivity extends AppCompatActivity {

    private AlertDialog alertDialogACCreationRetry;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        alertDialogACCreationRetry = new AlertDialog.Builder(this)
                .setTitle("Account creation failed")
                .setMessage("There was a problem connecting to the Network. Check your connection.")
                .setPositiveButton("Retry", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                }).create();
        alertDialogACCreationRetry.show();

        alertDialogACCreationRetry.show();
    }
}

I have tried putting the alertDialogACCreationRetry.show(); inside the Retry button but it still won't show. I have also tried putting alertDialogACCreationRetry.dismiss(); inside the Retry button and then calling alertDialogACCreationRetry.show(); outside, it still doesn't show. More so it is frightening that it doesn't give me an exception on reshowing it if that is not supposed to be allowed.

So, my question is this: Will I have to create a new Dialog every time after once it is dismissed(automatically) on pressing a button?

Manish Kumar Sharma
  • 12,982
  • 9
  • 58
  • 105

5 Answers5

11
public void showAlertDialog() {
    final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("Time");
    builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int i) {
            dialog.cancel();
            // call function show alert dialog again
            showAlertDialog();
        }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    final AlertDialog alert = builder.create();
    alert.show();
}

enter image description here

Linh
  • 57,942
  • 23
  • 262
  • 279
0

What probably is happening is that you're calling .show() before the dialog is dismissed, so it's a no-op. When you return from the retry button listener the dialog is dismissed.

PS this seems to agree with what I'm suggesting: https://stackoverflow.com/a/12110191/3286819

To make this work, you should call the show again after the network code has really retried the operation, probably in a background thread or something similar.

Finally, on the other hand probably it makes more sense to simply create the dialog again, as keeping the same dialog could create leaks in some cases.

Community
  • 1
  • 1
Xavier Rubio Jansana
  • 6,388
  • 1
  • 27
  • 50
  • Taking the network latency out of question, the thing is, I have tried calling dismiss() before calling show again() and not just once, I wrote alertDialogACCreationRetry.show() 7-8 times in series, it still didn't show – Manish Kumar Sharma Feb 09 '17 at 09:29
  • The problem is that what's really going on is that the dialog itself calls .dismiss() right after you return from your listener. A solution will be to call .show() later (for example using .postDelayed(), like so: `Handler handlerTimer = new Handler(); handlerTimer.postDelayed(new Runnable(){ public void run() { /* .show() */ }}, 100);` Anyway be aware that this is a hack. – Xavier Rubio Jansana Feb 09 '17 at 09:50
0

You don't need to recreate the alertDialogACCreationRetry. You can reuse it but you cannot show more than one alert dialogs with the same object at once. The show() method internally checks if the current alert dialog is already showing or not. If it is showing then it will return doing nothing else new dialog will be shown.

So make sure you call dismiss() before you call show() on dialog

This is what I have tried

@Nullable @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
  @Nullable Bundle savedInstanceState) {
getActivity().getSupportFragmentManager();

alertDialogACCreationRetry = new AlertDialog.Builder(getActivity())
    .setTitle("Account creation failed")
    .setMessage("There was a problem connecting to the Network. Check your connection.")
    .setPositiveButton("Retry", new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {

      }
    })
    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
      }
    }).create();
alertDialogACCreationRetry.show();

View view = inflater.inflate(R.layout.frag1, container, false);
TextView viewById = (TextView) view.findViewById(R.id.frag1_text);
viewById.setOnClickListener(new View.OnClickListener() {
  @Override public void onClick(View v) {
    alertDialogACCreationRetry.show();
  }
});
return view;
}

In the above alertDialogACCreationRetry.show() call inside onClick() will show the dialog if no dialog is showing already else it does nothing.

arjun
  • 3,514
  • 4
  • 27
  • 48
0

You can do something like this:

public class MainActivity extends AppCompatActivity {

private AlertDialog alertDialogACCreationRetry;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    alertDialogACCreationRetry = new AlertDialog.Builder(this)
            .setTitle("Account creation failed")
            .setMessage("There was a problem connecting to the Network. Check your connection.")
            .setPositiveButton("Retry", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            }).create();

    alertDialogACCreationRetry.show();

    alertDialogACCreationRetry.getButton(AlertDialog.BUTTON_POSITIVE).
        setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean yourLogicGoFine = false;
            if (yourLogicGoFine){
                alertDialogACCreationRetry.dismiss();
            }else{
                Toast.makeText(finalizarActivity, "Something was wrong",
                        Toast.LENGTH_SHORT).show();
            }
        }
    });
  }
}

Good luck!

Jose Angel Maneiro
  • 1,226
  • 9
  • 20
0

As others have stated, the reason calling show() doesn't make the dialog appear is because it is not dismissed yet. The method works by setting the dialog's visibility, not adding another dialog so you definitely cannot get 2 layers of the same dialog.

You can add a button to trigger showing the dialog to make sure the dialog is completely dismissed. Like this:

findViewById(R.id.show_button).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
       alertDialogACCreationRetry.show();
    }
});

Click on the button after you dismiss the dialog. It will surely re-appear.