Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6548291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:56:01+00:00 2026-05-25T11:56:01+00:00

I need to do some validations sequentially and some of them involve complex database

  • 0

I need to do some validations sequentially and some of them involve complex database operations.

So, I need to do this in a separated thread of UI Thread, ok?
But some validations show messages to user, what need confirmation and
when user confirm, the next validation should be call.

This code example explains what I want to implement:

void makeValidation1(){

    if(condition1Valid()){
        makeValidation2();

    }else{
        DialogInterface.OnClickListener onClick = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                makeValidation2();
            }
        };

        AlertDialog.Builder builder = new AlertDialog.Builder(this)
        .setMessage("really want to do this?")
        .setPositiveButton("Yes", onClick);

        builder.create().show();
    }
}

void makeValidation2(){

    if(condition2Valid()){
    }else{
        //...
    }
}

boolean condition1Valid() {
    // complex database Operations
    return false;
}
boolean condition2Valid() {
    //complex database Operations
    return false;
}

    //...


    void makeValidation9(){
         //...
    }

My question is: What the best way/pattern to implement this?

1 – Create one asyncTask for each validation? (I cant create only one AsyncTask, because confirmation messages can stop flux).

2 – Create a Runnable for each validation and create thread to run that when need call next validation?

3 – ???


edit
I tested this code @BinaryBazooka, but isnt work. Any help?

public class MainActivity extends Activity implements OnClickListener {

Thread mThread;
ProgressDialog mProgressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Button button = new Button(this);
    button.setText("Start");
    setContentView(button, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    button.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    mThread = new Thread(new Runnable() {
        @Override
        public void run() {
            validations();
        }
    });

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Start Thread?");
    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();

            mProgressDialog = new ProgressDialog(MainActivity.this);
            mProgressDialog.show();

            mThread.run();
        }
    });

    builder.create().show();
}

void validations(){
    //this method go on separated thread
    validation1();

    validation2();

    validation3();
}

void validation1(){

    if(true){
        final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

        builder.setMessage("Validation 1 failed. Go validation 2?");

        builder.setPositiveButton("Go", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                mProgressDialog.show();
                //if user confirm, continue validation thread
                mThread.notify();
            }
        });
        builder.setOnCancelListener(new OnCancelListener() {

            @Override
            public void onCancel(DialogInterface dialog) {
                //if user cancel, stop validation thread
                mThread.interrupt();
            }
        });

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mProgressDialog.hide();
                builder.create().show();
            }
        });

        try {
            synchronized (mThread) {
                //wait for user confirmation
                mThread.wait(); 
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

private void validation2() {

    if(true){
        final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

        builder.setMessage("validacao 2 failed. Go validation 3?");
        builder.setPositiveButton("Go", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                mProgressDialog.show();
                mThread.notify();
            }
        });
        builder.setOnCancelListener(new OnCancelListener() {

            @Override
            public void onCancel(DialogInterface dialog) {
                mThread.interrupt();
            }
        });

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mProgressDialog.hide();
                builder.create().show();
            }
        });

        try {
            synchronized (mThread) {
                mThread.wait();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

private void validation3() {
    Log.i("TAG", "<<<<<<<<<<  >>>>>>>>>>>>");
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(MainActivity.this, "finished", Toast.LENGTH_SHORT);
        }
    });
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:56:02+00:00Added an answer on May 25, 2026 at 11:56 am

    I would create a new thread and sleep it during these dialog calls, you can access the UI directly from within your runnable with..

    runOnUiThread(new Runnable() {                           
    
     public void run() {}
    
    });
    

    So something like..

    Thread someThread = new Thread(new Runnable() { 
    
        @Override
        public void run(){
            runOnUiThread(new Runnable() {                           
                public void run() 
                {
                    AlertDialog.Builder builder = new AlertDialog.Builder(context);                               
                    builder.setMessage(R.string.msg);
                    builder.setPositiveButton(R.string.btn_ok, new  DialogInterface.OnClickListener() {                 
                        @Override
                        public void onClick(DialogInterface dialog, int id) { 
                            dialog.dismiss();
                            someThread.notify();
                        }
                    });
                    AlertDialog alert = builder.create();       
                    alert.show();
                }
            });
        }
        someThread.wait();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need some help to solve this. I have a gridview and inside the gridview
Need some help with this problem in implementing with XSLT, I had already implemented
Need some help gathering thoughts on this issue. Our team is moving ahead with
I have asked this question before - but I have spent some time thinking
I'm trying to validate some POST data. One of the validations I need to
I have a very basic form, with some very basic validations (though I need
I need to add some validations before the user navigates away via an ASP.NET
I need to do some alert-message (like validations, etc), and I'm doing that with
I use a jquery validation plugin and I need to add some extra checking,
Need some help about with Memcache. I have created a class and want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.