Manish Kumar Sharma
Manish Kumar Sharma

Reputation: 13432

Showing the same AlertDialog again

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?

Upvotes: 6

Views: 5701

Answers (5)

Ngoc Nguyen
Ngoc Nguyen

Reputation: 51

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.

Upvotes: 0

Linh
Linh

Reputation: 60923

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

Upvotes: 11

arjun
arjun

Reputation: 3574

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.

Upvotes: 0

Jose Angel Maneiro
Jose Angel Maneiro

Reputation: 1316

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!

Upvotes: 0

Xavier Rubio Jansana
Xavier Rubio Jansana

Reputation: 6583

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.

Upvotes: 0

Related Questions