Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onCreateDialog doesn't refresh message

Tags:

android

I try to create AlertDialog. It works but setMessage doesn't refresh message. Code snippet below:

@Override
    protected Dialog onCreateDialog(int id) {
        super.onCreateDialog(id);
        switch (id) {
           case CONFIRM:                    
                confirmView = new LinearLayout(this);

                return new AlertDialog.Builder(this)
                        .setIcon(android.R.drawable.ic_dialog_info)
                        .setView(confirmView)
                        .setCancelable(false)
                        .setTitle("The WiFi")                      
                        .setMessage(infoMsg);
                        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                Functionality.StartWiFiManager(ControllerService.this);
                            }
                        })                         
                        .setNegativeButton("Cancel", new OnClickListener() {                            
                            @Override
                            public void onClick(DialogInterface dialog, int which) {                                    
                            }
                        })
                        .create();  
       }
}   

Invoking:

infoMsg = "My message";     
showDialog(CONFIRM); 

So when I change my infoMsg and invoke showDialog again, the message is the same. What am I doing wrong? Please, help me.

like image 568
Nolesh Avatar asked Dec 05 '22 18:12

Nolesh


1 Answers

You need to use 'onPrepareDialog' to reset information that changes every time the dialog is shown. onCreateDialog is only called once, then the dialog is held onto and re-used. onPrepareDialog is called every time that the dialog is shown.

Of course, both onCreateDialog and onPrepareDialog are deprecated and you are supposed to be using 'DialogFragment' class and the 'FragmentManager' instead. But if you (like me) are continuing to use the old APIs, then onPrepareDialog is what you want.

like image 185
Michael Kohne Avatar answered Dec 29 '22 04:12

Michael Kohne