我已经搜索了很多关于这个问题的修复,但显然我找不到一个.
好吧,顾名思义我有一个简单的Android应用程序,它有一个Webview.
public class MainActivity extends Activity {
protected FrameLayout webViewPlaceholder;
protected WebView webView;
final Context myApp = this;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize the UI
initUI();
}
protected void initUI()
{
// Retrieve UI elements
webViewPlaceholder = ((FrameLayout)findViewById(R.id.webViewPlaceholder));
// Initialize the WebView if necessary
if (webView == null)
{
// Create the webview
webView = new WebView(this);
webView.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(true);
webView.getSettings().setLoadsImagesAutomatically(true);
// Load the URLs inside the WebView, not in the external web browser
webView.setWebViewClient(new WebViewClient());
//webView.setWebChromeClient(new CustomChromeClient(this));
webView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result)
{
new AlertDialog.Builder(myApp)
.setTitle("javaScript dialog")
.setMessage(message)
.setPositiveButton(android.R.string.ok,
new AlertDialog.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
result.confirm();
}
})
.setCancelable(false)
.create()
.show();
return true;
};
public void onShowCustomView(View view, CustomViewCallback callback){
super.onShowCustomView(view, callback);
Log.i("ChromeCLient", "onshowCustomView");
new AlertDialog.Builder(myApp)
.setTitle("javaScript dialog")
.setMessage("ajbdlsakndsland")
.setCancelable(true)
.create()
.show();
}
@Override
public void onHideCustomView() {
super.onHideCustomView();
Log.i("ChromeCLient", "onhideCustomView");
}
});
webView.getSettings().setJavaScriptEnabled(true);
if (Build.VERSION.SDK_INT < 8) {
webView.getSettings().setPluginsEnabled(true);
} else {
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
}
// Load a page
webView.loadUrl("http://jsbin.com/ijixe4/3");
}
// Attach the WebView to its placeholder
webViewPlaceholder.addView(webView);
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
if (webView != null)
{
// Remove the WebView from the old placeholder
webViewPlaceholder.removeView(webView);
}
super.onConfigurationChanged(newConfig);
// Load the layout resource for the new configuration
setContentView(R.layout.activity_main);
// Reinitialize the UI
initUI();
}
@Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
// Save the state of the WebView
webView.saveState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
// Restore the state of the WebView
webView.restoreState(savedInstanceState);
}
}
我的问题如下.
在webview中加载的网站是一个包含简单iframe嵌入式YouTube视频的网站.
当我点击youtube视频上的FullScreen按钮时,即使我在我的清单中设置了它,它也会改变我的应用方向:screenOrientation =“portrait”.
退出FullScreen时,播放视频时会继续播放(在后台播放),即使由于方向更改而重新创建了webview.
当方向从横向变为纵向时,我已尝试在活动的onDestroy()方法中销毁webview.但它没有帮助.
我已经尝试将CustomWebChromeClient设置为webview并实现onShowCustomView()方法,但是除非我使用HTML5视频,否则显然不会调用onShowCustomView().
我甚至试图通过方向改变来保留webview的状态.
你们有没有其他方法来解决这个问题/错误?
我真的不想使用HTML5视频标签.
提前致谢.
解决方法:
只需覆盖onPause和onResume方法,并在webView上调用相同的方法,如下所示.
@Override
protected void onResume() {
super.onResume();
this.webView.onResume();
}
@Override
protected void onPause() {
super.onPause();
this.webView.onPause();
}