【发布时间】:2017-10-13 21:01:09
【问题描述】:
下面我发布了我当前的onRenderProcessGone。
- 在
if (!detail.didCrash()) {}中,实例变量“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; }
【问题讨论】:
标签: android android-webview webviewclient android-8.0-oreo