-1

I'm doing customized AlertDialog as indicated below:

public class Message {
    private static AlertDialog.Builder alertDialog;
    private final static Handler m_handler = new Handler() {
        @Override
        public void handleMessage(Message mesg) {
            throw new RuntimeException();
        }
    };

   public static void simpleMessage(String Message, String textButton, Context act){

       alertDialog = new AlertDialog.Builder(act);
       LayoutInflater inflater = (LayoutInflater) act.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
       View v = inflater.inflate(R.layout.message_simple,null);

       TextView tvMessageSimple = (TextView) v.findViewById(R.id.tv_message_simple);
       Button btnOKMessageSimple = (Button) v.findViewById(R.id.btn_ok_message_simple);
       tvMessageSimple.setText(message);
       btnOKMessageSimple.setText(textButton);

       alertDialog.setView(v);
       alertDialog.show();

       btnOKMessageSimple.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               m_handler.sendMessage(m_handler.obtainMessage());
           }
       });


       // loop till a runtime exception is triggered.
       try {
           Looper.loop();
       }
       catch(RuntimeException e2) {
           ;
       }

   }
}

The AlertDialog works very well and when the button is pressed, it should finish the AlertDialog.

When the button is pressed, the catch (RuntimeException e2) is executed, but the AlertDialog ends.

How should I finish it?

Thanks.

Fabián Romo
  • 319
  • 2
  • 14
  • Can you explain the question more? What are you trying to do? – Ahmed Hegazy Jan 11 '18 at 16:40
  • I'm doing customized AlertDialog. Look at this image: https://scontent.fuio10-1.fna.fbcdn.net/v/t1.0-9/26733397_10156180384597342_6380047193668359546_n.jpg?oh=7b6b59708bde30fa429ea253cf10d98e&oe=5AB34E21 – Fabián Romo Jan 11 '18 at 16:54
  • what is the purpose of the try/catch at the end ? – crgarridos Jan 11 '18 at 17:01
  • "Try" creates an infinite loop where it shows the AlertDialog of the previous image, when the button is pressed, an exception occurs to finish with "catch" – Fabián Romo Jan 11 '18 at 17:05
  • add `e2.printStackTrace();` and put the log obtained in the question so that we can understand the issue – Kartik Arora Jan 11 '18 at 17:08
  • I can comment or withdraw the try / catch and it still works, but what I want is to finish the AlertDialog when I press the button. – Fabián Romo Jan 11 '18 at 17:09

1 Answers1

1

When you build your alert dialog, you should save a reference in order to be able to dismiss it after using Dialog#dismiss(). method.

Look at this code:

final AlertDialog dialog = alertDialog.show();

btnOKMessageSimple.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        dialog.dismiss();
        m_handler.sendMessage(m_handler.obtainMessage());
    }
});

And just get rid of the try/catch at the end.

You can also refer to this question for more undertanding.

crgarridos
  • 8,758
  • 3
  • 49
  • 61