我想知道在我的Android应用中是否有多个webview实例正在创建。如果是,我该如何解决?
每当我调用loadURL时,我在Chrome上看到一个新的订单项,好像是否有一个新的WebView实例正在创建。 这是我的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webview);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (0 != (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE))
{ WebView.setWebContentsDebuggingEnabled(true); }
}
myWebView.setWebViewClient(new WebViewClient(){
public void onPageFinished(WebView view, String url) {
// Page is done loading;
_appRunning = true;
}
});
myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
myWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);
myWebView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
final WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setAppCachePath(f.getAbsolutePath());
webSettings.setAllowFileAccess(true);
webSettings.setAppCacheEnabled(true);
}
@Override
public void onResume(){
super.onResume();
if(!checkConnectivity()) return;
_appRunning = true;
WebView myWebView = (WebView) findViewById(R.id.webview);
String url = getString(R.string.app_url);
myWebView.loadUrl(url);
myWebView.onResume();
hideSystemUI();
}
@Override
public void onPause() {
super.onPause(); // Always call the superclass method first
stopPlayer();
//myWebView.onPause();
}
@Override
public void onStop() {
stopPlayer();
super.onStop();
}
private void stopPlayer(){
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("about:blank");
showSystemUI();
_appRunning = false;
}
答案 0 :(得分:1)
不要在stopPlayer()和onResume()中重新声明WebView。使用您在onCreate()中创建的相同实例。 您可以将该webView定义为字段并在onCreate()中初始化它并在任何地方使用它。
WebView myWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webview);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (0 != (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE))
{ WebView.setWebContentsDebuggingEnabled(true); }
}
myWebView.setWebViewClient(new WebViewClient(){
public void onPageFinished(WebView view, String url) {
// Page is done loading;
_appRunning = true;
}
});
myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
myWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);
myWebView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
final WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setAppCachePath(f.getAbsolutePath());
webSettings.setAllowFileAccess(true);
webSettings.setAppCacheEnabled(true);
}
@Override
public void onResume(){
super.onResume();
if(!checkConnectivity()) return;
_appRunning = true;
String url = getString(R.string.app_url);
myWebView.loadUrl(url);
myWebView.onResume();
hideSystemUI();
}
@Override
public void onPause() {
super.onPause(); // Always call the superclass method first
stopPlayer();
//myWebView.onPause();
}
@Override
public void onStop() {
stopPlayer();
super.onStop();
}
private void stopPlayer(){
myWebView.loadUrl("about:blank");
showSystemUI();
_appRunning = false;
}