Skip to content

Instantly share code, notes, and snippets.

@andreban
Created May 21, 2019 18:31
Show Gist options
  • Save andreban/e6aaab7536c20b32fb5f7d12c3697874 to your computer and use it in GitHub Desktop.
Save andreban/e6aaab7536c20b32fb5f7d12c3697874 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Crash"
android:layout_gravity="center"
android:onClick="crash"/>
</LinearLayout>
package com.test.hellowebviewjava;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.RenderProcessGoneDetail;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = findViewById(R.id.webview);
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean onRenderProcessGone(WebView view, RenderProcessGoneDetail detail) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return super.onRenderProcessGone(view, detail);
}
super.onRenderProcessGone(view, detail);
if (view != null) {
((ViewGroup) view.getParent()).removeView(view);
view.destroy();
view = null;
}
return true;
}
});
mWebView.loadUrl("https://www.example.com");
}
public void crash(View v) {
if (mWebView != null) {
mWebView.loadUrl("chrome://crash");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment