Search code examples
androidandroid-alertdialogonpause

How to dismiss an alertbox?


I want to dismiss my alert-box from my onPause() method because, I am checking the internet connection and if no connection is there I am showing an alert-box saying no connection. But when I minimize the app and relaunch it the previous alert-box is present there so in total two alert-boxes will be there I want to avoid that.So if I am dismissing it in the onPause() method the second one will not come up.

Can anybody help me with this, Thank you.

   if (!haveNetworkConnection) {
            // Toast.makeText(getApplicationContext(), "No Connection",
            // Toast.LENGTH_SHORT).show();
            advtlayout.setVisibility(View.GONE);
            System.out.println("no network");
            showGraphOverlay();
            hideOutputTextView();
            hideProgressBar();
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                    InterPreterWithAnimation.this);

            // Setting Dialog Title
            alertDialog.setTitle("No Network");

            // Setting Dialog Message
            alertDialog.setMessage("Turn on Wifi or Mobile data");

            // Setting Icon to Dialog
            // alertDialog.setIcon(R.drawable.);

            // Setting Positive "Yes" Button
            alertDialog.setPositiveButton("Settings",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int which) {

                            // Write your code here to invoke YES event

                            InterPreterWithAnimation.this
                                    .startActivity(new Intent(
                                            Settings.ACTION_SETTINGS));

                        }
                    });

            // Setting Negative "NO" Button
            alertDialog.setNegativeButton("Ok",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int which) {
                            // Write your code here to invoke NO event
                            dialog.cancel();
                        }
                    });

            // Showing Alert Message
            alertDialog.show();

Solution

  • Try creating AlertDialog this way:

            AlertDialog.Builder mAlertDialogBuilder = new Builder(mContext);
            mAlertDialogBuilder.setTitle("Your title here...");
            mAlertDialogBuilder.setMessage("Your message here...");
            mAlertDialogBuilder.setPositiveButton("Ok",
                    new DialogInterface.OnClickListener() {
    
                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
    
                        }
                    });
    
            mAlertDialog = mAlertDialogBuilder.create();
            mAlertDialog.show();
    

    Then, you can dismiss inside onPause() as:

    if(mAlertDialog.isShowing())
    {
        mAlertDialog.dismiss();
    }
    

    Also, declare your AlertDialog variable as your class member.