Android: Activity wait until boolean condition

android ? : condition
android if statement multiple conditions
java wait until condition
wait until boolean is true
kotlin wait until condition
or in android
java wait on atomic boolean
java wait for method to finish

Given a boolean condition isCondition which will be updated by an asynchronous task, how can I make an Android Activity wait on this condition without blocking execution?

In normal java, using Thread.sleep() inside a loop works well enough, but I am unsure of how safe this is to use in Android.

The asynchronous task that updates the isCondition variable is in an external library, which is abstracted away from my control. I would not like this endless poll to block or slow execution of other tasks, or take up too much processing, but rather release execution to other threads that may need to run.

(I am using API 25)

Its not a good idea to pause/sleep main UI thread because it would stop all UI interaction processing, often triggered "Application Not Responding" (ANR) dialog.

you could implement AsyncTask class, and get isCondition value in onPostExecute method, then do something as you want. official doc gives a good example .

Edit notice your comments, you could run a TimerTask to query isCondition value repeatedly.

java - Android: Activity wait until boolean condition, Its not a good idea to pause/sleep main UI thread because it would stop all UI interaction processing, often triggered "Application Not  Android Question Wait for Activity to finish before returning to previous Activity. Sub Activity_Pause(UserClosded As Boolean) and so forth until all is

we can:

// 1. use an callback interface:
public interface ICompleted{
    void execute(boolean succeeded);

// 2. pass an instance to asynchronous task runner
asynchTaskRunner (new ICompleted() {
        public void execute(boolean succeeded) {

// 3. run the callback from asynch task when nedded
iCompleted.execute(true or false);

Good luck )

Condition, Note that the check of the condition variable must be inside the synchronized block; Wait until either a condition becomes true or a timeout occurs type of waiting/sleeping is often done when the real intent is to wait until some operation​  I have a thread which wait for a boolean value to change like this: while(!value) { Thread.sleep(1000) } // Do some work after change of the value This is not my prefered way to do this, cause of massive CPU consumption. Is there any way to block the Thread, until the boolean value changes it state?

If I use Thread or Handler + while(true), it will cause ui stuck, so I use following code, cancel() is important, it will prevent run timertask again

public static void waitUntil(final Activity ctx, final WaitUntil wu) {
        new Timer().schedule(new TimerTask() {
            public void run() {
                ctx.runOnUiThread(new Runnable() {
                    public void run() {
                        if (wu.cond()) {

        }, 1000);

And WaitUntil interface

public interface WaitUntil {
    boolean cond();
    void todo();

WaitNotInLoop Because of spurious wakeups, Object.wait(), Advanced techniques for building custom waits in UiAutomator. approach to waiting for conditions with new classes, introduced in UiAutomator2. findObject​(By.text("foo")); // until element is gone SearchCondition<Boolean> isGone represents Android device itself and with its help you can wait for  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more Waiting till the async task finish its work

Waiting in UiAutomator, The Wait activity holds the process execution until an event occurs, a timeout or database event, or if explicitly closed, using the ActivityClose action in an action  The Until activity provides the same functionality that a do-until looping structure provides in programming languages. It executes a set of activities in a loop until the condition associated with the activity evaluates to true. You can specify a timeout value for the until activity in Data Factory. { "type": "Until", "typeProperties

Designing Waiting Activities, Android testing with Espresso - simple way to wait for activity to be Although you create the unusual situation of using a test-only inline fun <reified T : Activity> isVisible() : Boolean { val am = InstrumentationRegistry. @vikramvi, before u use explicit wait always remember to set 0 or minimal implicit wait, and reset it (Implicit wait) after your wait.until. This could be expected behaviour from selenium webdriver because ur explicit wait always user implicit wait as polling interval.

Android testing with Espresso, For a more substantial piece of work, you will want to save one piece of data without method that tries to serialize the crimes and returns a boolean indicating success. Also, add log statements so that you can easily confirm the operation. If you wait until onStop() or onDestroy(), you might miss your chance to save. boolean waitForWindowUpdate (String packageName, long timeout) Waits for a window content update event to occur. If a package name for the window is specified, but the current window does not have the same package name, the function returns immediately.

  • I would like it to keep polling the condition until it is true, at which point it should continue execution as per normal. However, I would not like this endless poll to block or slow execution of other tasks, or take up too much processing, but rather release execution to other threads that may need to run.
  • Thread.sleep on any thread but the main thread or the AsyncTask thread is fine. Using it on the main thread will cause the app to freeze, using it on the AsyncTask thread will starve other AsyncTasks. But on its own thread is ok. Its rarely the right thing to do though, its generally better to wait on a lock like a semaphore and signal it when the condition is true. If you want to notify the main thread, the best thing to do is post a message to a Handler made on the main thread when the condition is true.
  • I think when the isCondition variable is true in the asynchronous task then you can broadcast a message to the activity.
  • Thanks @GabeSechan. I think it will be in the main thread. I'm thinking about using a thread to poll the condition, and then signal the main thread. Can you answer this with some code snippets on how to set up this thread and the signal please?
  • @SonTruong The problem is the asynchronous task itself is abstracted away from my control, so I cannot write code in there to signal the activity.