0

Ho do i get my function to wait for being done with it's async task before returning the variable?

public boolean CheckOnline(){
    OnlineAsyncTask onlinetsk = new OnlineAsyncTask();
    onlinetsk.execute();
    return Online;
} 
1
  • 1
    Then it should be not async. Execute it in a normal thread and call join() on the thread object, to wait until it finishes. Make sure you don't block the UI-thread. Commented Mar 12, 2015 at 11:15

2 Answers 2

2

You can call get() to wait for the async task to complete and retrieve the result.

However that defeats the purpose of an async task - it's no longer asynchronous. Consider redesigning your app so that you don't need to wait for a result. Instead e.g. use a callback interface to notify that the async task has finished and a result is available.

Sign up to request clarification or add additional context in comments.

Comments

0
String str_result= new OnlineAsyncTask().execute().get();

This will make it wait till it returns the value

1 Comment

Would this also then run on the main thread and not on a separate thread?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.