Search code examples
androidandroid-runonuithread

Thread doesn't execute a Dialog?


I have a Dialog and a Thread. When thread finished I want to execute the Dialog. The problem is sometimes execute and sometimes does not. I can not understand why this happens.

How can I solve it ?

/** display dialog */
private void showDialog(int status){
    final Dialog dialog = new Dialog(getView().getContext());
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    dialog.getWindow().setFlags(LayoutParams.FLAG_FULLSCREEN, LayoutParams.FLAG_FULLSCREEN);                        
    dialog.setContentView(R.layout.custom_dialog);
    dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    dialog.setCancelable(false);
    ImageView ivCustomDialog = (ImageView) dialog.findViewById(R.id.ivCustomDialog);

    Button dialogButton = (Button) dialog.findViewById(R.id.btnProsseguir);
    dialogButton.setOnClickListener(new OnClickListener() {             
        @Override
        public void onClick(View v) {                                           
            dialog.dismiss();                   
        }
    });             

    dialog.show();                      
}

Thread

new Thread(){
    public void run(){
        int count = 0;
        while(count < 100){
            if(count >= 100){                                                       
                getActivity().runOnUiThread(new Runnable() {                                                        
                    @Override
                    public void run() {                                                             
                        showDialog(1);  
                        interrupt();
                    }
                });                                                         
            }
            count++;
        }
    }                                           
}.start();

Solution

  • its better you pass a valid context... the problem exist do to in proper passing of context.. its better you use AsyncTask...

    http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html