android - How to wait for webview.post to complete? -


in below code, outside printed before inside. want order inside first , outside. how ensure runnable finished before second log reached?

webview.post(new runnable() {     @override     public void run() {         log.d("check", "inside");     } });  // code needed here  log.d("check", "outside"); 

some code must inserted comment achieve this.

edit: doing work in background service.

[p.s.: curious why doing this, because unless add webview.post, keep getting following error: "all webview methods must called on same thread.". anyway, shouldn't affect answering question.]

you might try using countdownlatch:

final countdownlatch latch = new countdownlatch(1);  webview.post(new runnable() {     @override     public void run() {         log.d("check", "inside");         latch.countdown();     } });  // code needed here latch.await();  log.d("check", "outside"); 

however, wouldn't want use latch.await() on ui thread, that's blocking method. if wanted run on ui thread best replace latch.countdown() call callback method, in turn run log.d("check", "outside").


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -