开发者

Does WebView's loadUrl method run on UI thread?

I am wondering how does webview load a particular URL. Does it create a new thread or load the URL in the same thread i.e. UI thread? The reason I am asking this is I am facing some weird wakeup lock issue when I launch an Activity from current Activity (in current Activity's onCreate method) which creates a WebView in it's onCreate method and loads a URL using loadUrl method. So when I am done with this activity and go back to the Activity which launched this is restarted because wakeup lock time was expired.

I googled it and found out that if onCreate method of an Activity takes too long then this type of issue might occur. Have any of you faced this kind of issue involving a WebView? Any kind of help would be really appreciated.

Here is sample code.

Activity A:
class ActivityA {
  onCreate(){
    initializeSomething();
    registerSomeEvents();
    startSomething();
  }
  onSomeListener(){
    if(someFlag) {
      startActivity(ActivityB);
      //this will开发者_JAVA技巧 pause the current activity i.e. ActivityA
    }
  }
}
Activity B:
class ActivityB(){
  onCreate() {
    if(someFlag){ //someFlag is coming in intent
     helper = createHelperX();
    } else {
     helper = createHelperY();
    }
    helper.pleaseHelp(); //this does the rest of thing using activity instance
}

HelperX:

class HelperX(){ createHelper() { //setup layout views etc, no WebView here //and wait for user interaction //register some events } onSomeEvent(){ if(someOtherFlag) { startActivityForResult(ActivityB); //and pass some flag, now this flag will trigger HelperY } }

HelperY: class HelperY(){ createHelper() { //setup views etc , this contains a WebView and calls loadURL. } }


The loadUrl method in WebView uses the Handler mechanism i.e it will run in UI thread. You should stop the data loading by calling webView.stopLoading() when the activity is paused/stopped.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜