java - What is the best way to wait until a runnable on the main thread completes in Android? -
i need collect username , password user inside webviewclient#shouldinterceptrequest, must block webview io thread until user supplies username , password on main thread. best way wait until runnable completes?
my current favorite way (exceptions , timeouts omitted brevity):
final countdownlatch countdownlatch = new countdownlatch(1); new handler(looper.getmainlooper()).post(new runnable() { public void run() { callsomethingwithasynccallback(new runnable() { public void run() { countdownlatch.countdown(); } }); } }); countdownlatch.await(); something uses executorservices seems better since can use future#get block. however, there no executorservice runs on main thread, , using 1 executors bounce main thread seems wasteful. thoughts?
please use asynctask rather using runnable.
- the asynctask executes in doinbackground() inside of thread, not have access gui views are.
- preexecute() , postexecute() offer access gui before , after heavy lifting occurs in new thread, can pass result of long operation postexecute() show results of processing.
Comments
Post a Comment