关闭从WebView打开的全屏视频后的白屏

时间:2016-01-11 10:05:48

标签: android webview youtube

我有WebView嵌入了youtube视频。我使用简单的对话框解决方案实现了全屏模式,如下所示:

webView.setWebChromeClient(new CustomWebChromeClient());

public class CustomWebChromeClient extends WebChromeClient {
    @Override
    public void onShowCustomView(View view, CustomViewCallback callback) {
        Dialog dialog = new Dialog(ArticleDetailsActivity.this, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
        view.setBackgroundColor(getResources().getColor(R.color.black));
        dialog.setContentView(view);
        dialog.show();
    }

    @Override
    public void onHideCustomView() {
        super.onHideCustomView();
    }
}

除了使用后退按钮关闭视频时,它的效果非常好,因为全屏视频消失,白色全屏覆盖保留在屏幕上,直到另一个后退按钮使用。 我试图变得聪明,在onBackPressed()内做了onHideCustomView(),但随后整个活动就完成了。

如何摆脱白色的帷幕?

1 个答案:

答案 0 :(得分:2)

有人挖掘了android代码并找到了解决方案:

    public class CustomWebChromeClient extends WebChromeClient {

    @Override
    public void onShowCustomView(View view, final CustomViewCallback callback) {
        Dialog dialog = new Dialog(ArticleDetailsActivity.this, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
        view.setBackgroundColor(getResources().getColor(R.color.black));
        dialog.setContentView(view);
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                callback.onCustomViewHidden();
                chromeWebClient.onHideCustomView();
            }
        });
        dialog.show();
    }

    @Override
    public void onHideCustomView() {
        super.onHideCustomView();
    }
}

已连接问题 - 已解决: 在重新打开活动并再次播放视频时,有时会在HTML5VideoView.reprepareData时发生此次崩溃,或在调用HTML5VideoView.isPlayingwebView.onPause()这似乎是另一个问题......

最终注释

为了使WebView正常工作而不泄漏内存,您应该在WebViewMethodsActivity生命周期回调中调用相应的Fragment,如下所示Activity(可能某种方式类似于Fragment):

@Override
protected void onCreate(Bundle savedInstanceState) {
    if (savedInstanceState == null) {
        webView.restoreState(savedInstanceState);
    } 
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putBoolean(BundleKeys.HAS_PHOTOS, hasPhotos);
    outState.putLong(BundleKeys.ARTICLE_ID, articleId);
    webView.saveState(outState);
}

@Override
protected void onResume() {
    webView.onResume();
}

@Override
protected void onPause() {
    super.onPause();
    webView.onPause();
}

@Override
protected void onStop() {
    super.onStop();
    webView.stopLoading();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    webView.destroy();
}