Question

I'm new to Android, only three days mostly studying the basics. I ended up with this code while studying the creation of an android alert dialog:

OnClickListener oclBtnOk = new OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
                alertDialog.setTitle("Title");
                alertDialog.setMessage("Message");
                alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int which) {
                      // TODO Add your code for the button here.
                   }
                });
                alertDialog.show();            
            }
          };        

But I still have somethings to get known:

1- First time I made the code I was using AlertDialog alertDialog = new AlertDialog.Builder(this).create(); instead AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();. The first code wans't compiling, so, what exactly is the difference? That means the dialog builder is son or has some dependency with the MaiActivity? That means the listener from this dialog is inside the main activity?

2- Is this really the correct way to create a simple alert dialog? 'cause in my console I saw one red line saying "ActivityManager: Warning: Activity not started, its current task has been brought to the front" and also studying through http://developer.android.com/guide/topics/ui/dialogs.html what they recommend is to use AlertDialogFragment as container of my dialog..

And my last question is a little bit more complicated but a simple answer as "Yes" to lead me I a deeper research would help- android has some sort of EDT (Event Dispatch Thread) since is based on java? I mean, to process graphics (like progress bars) should I separate them into another thread?

Was it helpful?

Solution

  1. AlertDialog.Builder requires a Context. The code is in an OnClickListener anonymous subclass and it is not a Context. this refers to the subclass instance. To refer to the this of the parent activity class, it's scoped as MainActivity.this, and activities are Contexts.

  2. It's all right at this point of your learning curve. The "Warning: Activity not started, its current task has been brought to the front" is nothing to worry about - the app was already running and was just brought to front, not re-launched.

  3. Android does not run AWT and there's no EDT by that name. However, the main thread (also called the UI thread) does something similar.

OTHER TIPS

To answer your first question:

AlertDialog alertDialog = new AlertDialog.Builder(this).create(); 

The code is running in an anonymous class, so this does not reference an object derived from Context. Adding the MainActivity. makes the argument reference an Activity which is derived from Context

Your second question: Is this really the correct way....? It is certainly one acceptable way. There are others.

The message you are seeing about bringing the activity to the front is not related to dialogs. You may eliminate it by exiting from your application before starting a new debug session.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top