Android onRenderProcessGone(WebView视图,RenderProcessGoneDetail细节)示例
下面是我在RenderProcess Gone上发布的当前Android onRenderProcessGone(WebView视图,RenderProcessGoneDetail细节)示例,android,android-webview,webviewclient,android-8.0-oreo,Android,Android Webview,Webviewclient,Android 8.0 Oreo,下面是我在RenderProcess Gone上发布的当前 在中,如果(!detail.didcash()){}实例变量“view”保证为空,则可以安全地重新初始化它。我应该自己重新初始化它还是系统会这样做 您能否指定应用程序如何继续执行的逻辑示例?如何更优雅地处理碰撞 @TargetApi(Build.VERSION_CODES.O) @Override public boolean onRenderProcessGone(WebView view, RenderPro
中,如果(!detail.didcash()){}
实例变量“view”保证为空,则可以安全地重新初始化它。我应该自己重新初始化它还是系统会这样做 @TargetApi(Build.VERSION_CODES.O)
@Override
public boolean onRenderProcessGone(WebView view, RenderProcessGoneDetail detail) {
// WebViewClient.onRenderProcessGone was added in O.
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return false;
}
super.onRenderProcessGone(view, detail);
if (!detail.didCrash()) {
// Renderer was killed because the system ran out of memory.
// The app can recover gracefully by creating a new WebView instance
// in the foreground.
Log.e("MY_APP", "System killed the WebView rendering process " +
"to reclaim memory. Recreating...");
if (view != null) {
((ViewGroup)view.getParent()).removeView(view);
view.destroy();
view = null;
}
// By this point, the instance variable "view" is guaranteed
// to be null, so it's safe to reinitialize it.
return true; // The app continues executing.
}
// Renderer crashed because of an internal error, such as a memory
// access violation.
Log.e("MY_APP", "The WebView rendering process crashed!");
// In this example, the app itself crashes after detecting that the
// renderer crashed. If you choose to handle the crash more gracefully
// and allow your app to continue executing, you should 1) destroy the
// current WebView instance, 2) specify logic for how the app can
// continue executing, and 3) return "true" instead.
return false;
}
@TargetApi(Build.VERSION\u CODES.O)
@凌驾
公共布尔onRenderProcessGone(WebView视图,RenderProcessGoneDetail详细信息){
//WebViewClient.onRenderProcessGone已添加到O中。
if(Build.VERSION.SDK\u INT
我已经实现了这个方法,并且没有区分渲染器崩溃还是被系统杀死。就应用程序而言,结果是相同的-webview不可用,如果此方法返回
false
,应用程序也将被终止
我在创建片段/活动期间初始化的片段(或活动)中维护一个对WebView的引用和两个布局根视图
m_homeWebSwipe = v.findViewById(R.id.homeWebViewSwipe);
m_homeWebView = v.findViewById(R.id.homeWebView);
initializeWebView(m_homeWebView);
我的WebView是SwiperFreshLayout的子视图。布局XML并不重要,但仅供参考:
<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/homeWebViewSwipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/homeWebView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</WebView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
通过安排WebView加载URL,可以在onRenderProcessGone
方法中测试代码chrome://crash
@Override
public boolean onRenderProcessGone(final WebView view, RenderProcessGoneDetail detail) {
// Renderer was killed or died, recreate the webview
Log.e("HomeWebView", "web content rendering process killed - resetting WebView: " + view.hashCode());
// Only handle our WebView
if (m_homeWebView.equals(view)) {
// Get the parent container of the inflated layout
ViewGroup container = (ViewGroup) m_homeWebSwipe.getParent();
ViewGroup.LayoutParams params = container.getLayoutParams();
// Remove the inflated view from the container and cleanup
// the dead webview specifically (if it is not GC'ed it will cause
// problems later, the next time the renderer dies)
container.removeView(m_homeWebSwipe);
m_homeWebSwipe = null;
m_homeWebView = null;
view.destroy();
// Reinflate the view layout and add it back into the container
View v = getLayoutInflater().inflate(R.layout.home_webview_screen, container, false);
m_homeWebSwipe = v.findViewById(R.id.homeWebViewSwipe);
m_homeWebView = v.findViewById(R.id.homeWebView);
// Initialise webview here, same as when it was originally created
initializeWebView(m_homeWebView);
assert(getActivity() != null);
getActivity().setContentView(v, params);
// reload the displayed page, or load a new page here
reloadPage();
return true; // The app continues executing.
}
return false; // the app is killed
}