Close AlertDialog

0

I open the dialog and you can see that I send a call to take a photo or to select it from the gallery. The problem is that once the photo is uploaded the AlertDialog is still open, of course you can add a button like cancel with a simple dismiss but I need to close it when the activity of gallery or photos is closed. that is ... how can I close the Alertdialog from outside?

private void seleccionarMedio() {
    final AlertDialog.Builder dialog = new AlertDialog.Builder(this);

    dialog.setTitle("Selecciona una foto");
    dialog.setMessage("Puedes tomar una foto o cargar una antes previamente tomada");
    LayoutInflater inflater = LayoutInflater.from(this);
    View login_layout = inflater.inflate(R.layout.vista_foto,null);
    LinearLayout Galeria = (LinearLayout)login_layout.findViewById(R.id.botonGaleria);
    LinearLayout Tomar = (LinearLayout)login_layout.findViewById(R.id.botonTomar);

    dialog.setView(login_layout);
    Galeria.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
              Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            intent.setType("image/*");
            startActivityForResult(intent.createChooser(intent, "Selecciona app de imagen"), SELECT_PICTURE);
              //Cerrar dialog en esta parte.

        }
    });

    Tomar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            openCamera();
        }
    });


    dialog.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.dismiss();
        }
    });

    dialog.show();
}
    
asked by DoubleM 12.01.2018 в 08:31
source

1 answer

2

The method AlertDialog.Builder#show() returns an object of type AlertDialog that has the method dismiss() .

Save the result reference to close the AlertDialog out of it:

private AlertDialog _dialog;
private void seleccionarMedio() {
    final AlertDialog.Builder dialog = new AlertDialog.Builder(this);

    //...

    dialog.setView(login_layout);
    Galeria.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            intent.setType("image/*");
            startActivityForResult(intent.createChooser(intent, "Selecciona app de imagen"), SELECT_PICTURE);

            // verificamos que el objeto no sea null
            if(NombreActividadActual.this._dialog!=null){
                NombreActividadActual.this._dialog.dismiss()
            }

        }
    });

    //...

    _dialog = dialog.show();
}
    
answered by 12.01.2018 в 13:16