0

I am trying to open a AlertDialog. When this AlertDialog is opened, the threads need to wait for user input in order to continue its program. I read that I need to lock the object thats need to wait and notified. When I run this code on my phone. The alertdialog won't show, and it looks like the app is looping, because after a few seconds I get a message that the app isn't responding. Below you will find the code I wrote.. By the way. I am a virgin to android programming. So please be gentle :P

public class EditTagActivity extends Activity{  

AlertDialog alertDialog;

Runnable h = new Runnable()
{
    @ Override
    public void run() 
    {
        alertDialog.show();
        synchronized(g)
        {
            try 
            {
                Log.d("Threads", "g.wait()");
                g.wait();
            } 
            catch (InterruptedException e) 
            {
                e.printStackTrace();
            }
        }           
    }
};

Runnable g = new Runnable() 
{
    @Override
    public void run() 
    {
        Log.d("Threads", "createAlertDialog()");
        createAlertDialog();
        runOnUiThread(h);
    }
};
public AlertDialog alert;
Runnable test = new dialogManager();
@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()");
    g.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(g)
            {
                Log.d("Threads", "g.notifyAll");    
                g.notifyAll();
            }
        }
    });
    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
    {
        public void onClick(DialogInterface dialog, int which) 
        {
            synchronized(g)
            {
                Log.d("Threads", "g.notifyAll");
                g.notifyAll();
            }
        }
    });
    alertDialog = alert.create();
}

}

Robert
  • 5,278
  • 43
  • 65
  • 115
The Stompiest
  • 318
  • 2
  • 15
  • You're doing the waiting on the UI thread itself, that won't work - the entire UI is just stuck. What exactly are you trying to achieve with all the waiting..? – nitzanj Nov 07 '14 at 14:35
  • hmm.. okay.. i changed my runnables into threads.. When a alertdialog is showed.. The thread that opened the alert dialog needs to wait for the user input.. – The Stompiest Nov 07 '14 at 14:56
  • http://developer.android.com/guide/components/processes-and-threads.html – Ben Pearson Nov 07 '14 at 15:40

1 Answers1

1

Instead of Java's Thread and Runnable approach, you should use Android's AsyncTask, It helps you resolve this more simply by overriding the onPreExecute and onPostExecute methods to handle things just before and after running the code in background.

This post has a good example for using the AsyncTask class.

Community
  • 1
  • 1
Abdallah Alaraby
  • 2,222
  • 2
  • 18
  • 30
  • You should [read more](http://developer.android.com/guide/components/processes-and-threads.html) about Threading in Android, it's important that you understand the usage of UI Thread and the background Threads. – Abdallah Alaraby Nov 07 '14 at 14:57
  • Okay.. I will try that.. I will let you know when I got it working.. thanks in advance though – The Stompiest Nov 07 '14 at 14:59