Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 940
Manish Kumar Sharma Avatar asked Feb 09 '17 09:02

Manish Kumar Sharma


1 Answers

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

like image 111
Linh Avatar answered Sep 20 '22 00:09

Linh