Jacek Kwiecień
Jacek Kwiecień

Reputation: 12637

White screen after closing fullscreen video opened from WebView

I have a WebView with embedded youtube video. I've implemented fullscreen mode using simple dialog solution like this:

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();
    }
}

It works quite well except the moment of closing the video using back button because then the fullscreen vide disappears and the white fullscreen overlay stays on the screen until another back button use. I tried to be smart and did onBackPressed() inside onHideCustomView() but then whole activity gets finished.

How to get off rid that white curtain?

Upvotes: 4

Views: 1347

Answers (1)

Jacek Kwiecień
Jacek Kwiecień

Reputation: 12637

Did some digging into android code and found the solution:

    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();
    }
}

CONNECTED ISSUE - SOLVED: Turns out this crashes sometimes with HTML5VideoView.reprepareData when reopening the activity and playing the video again or HTML5VideoView.isPlaying when calling webView.onPause() which seems to be another issue...

FINAL NOTE

In order to WebView work well and not leaking memory you should call respective WebViewMethods in Activity or Fragment lifecycle callbacks as following for the Activity (probably somehow similar for the 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();
}

Upvotes: 2

Related Questions