Reputation: 331
Okay, so I wrote this code, where I use a thread to open some alert dialogs. The problem is that the thread doesn't wait for the first dialog to close before opening the second dialog. As you can see in the code I used wait()
between opening the two dialogs and usenotify()
in the onClick events of the buttons in the dialogs. I use the runOnUidThread()
function to show the created dialogs. For some reason, when I open the activity the dialogs won't open and the app will have a black screen and the app will eventualy stop working. When I comment out the wait()
statements (including the try and catch statements), the activity shows both the alert dialogs after each other. So is it even possible to wait for user input like this? Or am I doing something completly wrong. Thank you. HEre is my code :
public class EditTagActivity extends Activity
{
AlertDialog alertDialog1, alertDialog2;
Thread dialogManager = new Thread()
{
@Override
public void run()
{
runOnUiThread(showDialog1);
try
{
synchronized(dialogManager)
{
Log.d("Threads", "wait()");
dialogManager.wait();
}
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
runOnUiThread(showDialog2);
try
{
synchronized(dialogManager)
{
Log.d("Threads", "wait()");
dialogManager.wait();
}
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
Thread showDialog1 = new Thread()
{
@Override
public void run()
{
Log.d("Threads", "alertDialog1.show();");
alertDialog1.show();
}
};
Thread showDialog2 = new Thread()
{
@Override
public void run()
{
Log.d("Threads", "alertDialog2.show();");
alertDialog2.show();
}
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_tag);
Log.d("Threads", "setup()");
setup();
}
void setup()
{
Log.d("Threads", "g.run()");
createAlertDialog();
dialogManager.run();
}
void createAlertDialog()
{
Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Alert");
alert.setMessage("Flaq");
alert.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
synchronized(dialogManager)
{
dialogManager.notifyAll();
}
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
synchronized(dialogManager)
{
dialogManager.notifyAll();
}
}
});
alertDialog1 = alert.create();
alertDialog2 = alert.create();
}
}
Upvotes: 1
Views: 6767
Reputation: 3344
Why are you using threads to wait for user input? A dialog has to be run on the UI-thread as stated in the android training guide: "Every app has its own special thread that runs UI objects such as View objects; this thread is called the UI thread."
A dialog will occupy the UI-thread until it is dismissed. On close you can process the user input on a background thread.
Here are some good references:
https://developer.android.com/training/multiple-threads/communicate-ui.html
http://developer.android.com/guide/topics/ui/dialogs.html
void createAlertDialog()
{
Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Alert");
alert.setMessage("Flaq");
alert.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
/*
Handle user input on other thread. With for example AsyncTask
*/
alertDialog2 = alert.create();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
/*
Handle user input on other thread. With for example AsyncTask
*/
}
});
alertDialog1 = alert.create();
}
Upvotes: 0
Reputation: 2249
Yes, just call the dialog.show() of the second dialog in the onClick of the first dialog's setPositiveButton. This way the second dialog will be forced to wait until the first one is dismissed.
Upvotes: 2