Like Modal Dialog android

EsmaeelQash

I want to get the Gender of the user before he can continue using the program, thus, I used Asyc class

 class conectNet extends AsyncTask<Void,Void,Void>

I did first to avoid using network inside main layout. and then I Show a dialogue that asks the user to Identify his gender:

protected void onPostExecute(Void result){ // this method can use UI as mentioned in its docs
...

...

the main point of my question

I make dialogue and want to stop the thread until I get user response:

 ...
    ...
     AlertDialog.Builder builder = new AlertDialog.Builder(SendRandom.this);
    builder.setTitle("gender");
     builder.setMessage(R.string.Gender_dialogQ); //Gender_dialogQ=select your gender!
    // Set up the buttons
     builder.setPositiveButton("Male", new DialogInterface.OnClickListener() { 
       @Override
       public void onClick(DialogInterface dialog, int which) {
         userGender=Gender.MALE;//put value on mainClass proprty (userGender)
    synchronized (Thread.currentThread()){
        Thread.currentThread().notify(); // here is the POINT : resume the thread when selection done
        } } });
        builder.setNegativeButton("Female", new DialogInterface.OnClickListener() {
         @Override
        public void onClick(DialogInterface dialog, int which) {
         userGender=Gender.FEMALE;
         dialog.cancel();
         synchronized (Thread.currentThread()){
        Thread.currentThread().notify();}}});
        builder.show();// show dialogue and then pause the thread by next line
         try {
        synchronized( Thread.currentThread()){
        Thread.currentThread().wait();}
                    } catch......
    ....

this make may android program to halt And close

any suggestions??

codeMagic

You don't need all of the Threads, synchronized, etc... simply use setCancelable on your AlertDialog.Builder. This will force the user to select and option before continuing or closing the Dialog.

 AlertDialog.Builder builder = new AlertDialog.Builder(SendRandom.this);
 builder.setTitle("gender");
 builder.setCancelable(false);  // here
 builder.setMessage(R.string.Gender_dialogQ); //Gender_dialogQ=select your gender!
 // Set up the buttons
 ...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related