{"id":134,"date":"2019-12-02T09:55:40","date_gmt":"2019-12-02T01:55:40","guid":{"rendered":"http:\/\/cf3b5.com\/blog\/?p=134"},"modified":"2019-12-02T09:55:40","modified_gmt":"2019-12-02T01:55:40","slug":"webview%e7%9a%84%e5%86%85%e5%ad%98%e6%b3%84%e6%bc%8f%e6%a0%b9%e6%ba%90%e8%a7%a3%e5%86%b3","status":"publish","type":"post","link":"http:\/\/cf3b5.com\/blog\/?p=134","title":{"rendered":"Webview\u7684\u5185\u5b58\u6cc4\u6f0f\u6839\u6e90\u89e3\u51b3"},"content":{"rendered":"\n<p>\u7b80\u5355\u6765\u8bf4\uff0c\u8fd9\u4e2a\u5185\u5b58\u6cc4\u6f0f\u662f\u4f60\u9000\u51fa\u4e86App\uff0c\u4f46\u662fwebview\u8fd8\u6ca1\u6709\u9500\u6bc1\uff0c\u6709\u4e2a\u5224\u65ad\u6807\u51c6\u662f\u5f00\u542f\u4e86webview\u8fdc\u7a0bdebug\u540e\uff0c\u5c31\u7b97\u9000\u51fa\u4e86app\uff0c\u4f60\u4f1a\u53d1\u73b0debug\u9875\u9762\u8fd8\u6302\u7740\u8fd9\u4e2awebview\uff0c\u5728\u5f00\u542fapp\u7684\u65f6\u5019\uff0cwebview\u53c8\u4f1a\u591a\u4e00\u4e2a\u5b9e\u4f8b\u51fa\u6765\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"518\" height=\"439\" src=\"http:\/\/cf3b5.com\/blog\/wp-content\/uploads\/2019\/12\/image.png\" alt=\"\" class=\"wp-image-135\" srcset=\"http:\/\/cf3b5.com\/blog\/wp-content\/uploads\/2019\/12\/image.png 518w, http:\/\/cf3b5.com\/blog\/wp-content\/uploads\/2019\/12\/image-300x254.png 300w\" sizes=\"(max-width: 518px) 100vw, 518px\" \/><figcaption>\u8b6c\u5982\u50cf\u8fd9\u6837\u5b50\u2026\u2026<\/figcaption><\/figure>\n\n\n\n<p>\u7f51\u4e0a\u627e\u4e86\u4e00\u5708\u4e4b\u540e\uff0c\u53d1\u73b0\u8fd9\u4e2a\u529e\u6cd5\u662f\u6700\u9760\u8c31\u7684\uff01\u4ece\u6839\u6e90\u4e0a\u6765\u8bf4\uff0cwebview\u5f15\u8d77\u7684\u5185\u5b58\u6cc4\u6f0f\u4e3b\u8981\u662f\u56e0\u4e3aorg.chromium.android_webview.AwContents \u7c7b\u4e2d\u6ce8\u518c\u4e86component callbacks\uff0c\u4f46\u662f\u672a\u6b63\u5e38\u53cd\u6ce8\u518c\u800c\u5bfc\u81f4\u7684\u3002<\/p>\n\n\n\n<p>org.chromium.android_webview.AwContents \u7c7b\u4e2d\u6709\u8fd9\u4e24\u4e2a\u65b9\u6cd5 onAttachedToWindow \u548c onDetachedFromWindow\uff1b\u7cfb\u7edf\u4f1a\u5728attach\u548cdetach\u5904\u8fdb\u884c\u6ce8\u518c\u548c\u53cd\u6ce8\u518ccomponent callback\uff1b<br>\n\u5728onDetachedFromWindow() \u65b9\u6cd5\u7684\u7b2c\u4e00\u884c\u4e2d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">if (isDestroyed()) return;<\/pre>\n\n\n\n<p>\u5982\u679c isDestroyed() \u8fd4\u56de true \u7684\u8bdd\uff0c\u90a3\u4e48\u540e\u7eed\u7684\u903b\u8f91\u5c31\u4e0d\u80fd\u6b63\u5e38\u8d70\u5230\uff0c\u6240\u4ee5\u5c31\u4e0d\u4f1a\u6267\u884cunregister\u7684\u64cd\u4f5c\uff1b\u6211\u4eec\u7684activity\u9000\u51fa\u7684\u65f6\u5019\uff0c\u90fd\u4f1a\u4e3b\u52a8\u8c03\u7528 WebView.destroy() \u65b9\u6cd5\uff0c\u8fd9\u4f1a\u5bfc\u81f4 isDestroyed() \u8fd4\u56de true\uff1bdestroy()\u7684\u6267\u884c\u65f6\u95f4\u53c8\u5728onDetachedFromWindow\u4e4b\u524d\uff0c\u6240\u4ee5\u5c31\u4f1a\u5bfc\u81f4\u4e0d\u80fd\u6b63\u5e38\u8fdb\u884cunregister()\u3002<br>\n\u7136\u540e\u89e3\u51b3\u65b9\u6cd5\u5c31\u662f\uff1a\u8ba9onDetachedFromWindow\u5148\u8d70\uff0c\u5728\u4e3b\u52a8\u8c03\u7528destroy()\u4e4b\u524d\uff0c\u628awebview\u4ece\u5b83\u7684parent\u4e0a\u9762\u79fb\u9664\u6389\u3002<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">ViewParent parent = mWebView.getParent();\nif (parent != null) {\n    ((ViewGroup) parent).removeView(mWebView);\n}\n\nmWebView.destroy();<\/pre>\n\n\n\n<p>\u5b8c\u6574\u7684activity\u7684onDestroy()\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">@Override\nprotected void onDestroy() {\n    if( mWebView!=null) {\n\n        \/\/ \u5982\u679c\u5148\u8c03\u7528destroy()\u65b9\u6cd5\uff0c\u5219\u4f1a\u547d\u4e2dif (isDestroyed()) return;\u8fd9\u4e00\u884c\u4ee3\u7801\uff0c\u9700\u8981\u5148onDetachedFromWindow()\uff0c\u518d\n        \/\/ destory()\n        ViewParent parent = mWebView.getParent();\n        if (parent != null) {\n            ((ViewGroup) parent).removeView(mWebView);\n        }\n\n        mWebView.stopLoading();\n        \/\/ \u9000\u51fa\u65f6\u8c03\u7528\u6b64\u65b9\u6cd5\uff0c\u79fb\u9664\u7ed1\u5b9a\u7684\u670d\u52a1\uff0c\u5426\u5219\u67d0\u4e9b\u7279\u5b9a\u7cfb\u7edf\u4f1a\u62a5\u9519\n        mWebView.getSettings().setJavaScriptEnabled(false);\n        mWebView.clearHistory();\n        mWebView.clearView();\n        mWebView.removeAllViews();\n        mWebView.destroy();\n\n    }\n    super.on Destroy();\n}<\/pre>\n\n\n\n<p>\u89e3\u51b3\uff01<\/p>\n\n\n\n<p>\u539f\u6587\uff1a<a href=\"https:\/\/blog.csdn.net\/xygy8860\/article\/details\/53334476\">https:\/\/blog.csdn.net\/xygy8860\/article\/details\/53334476<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u5355\u6765\u8bf4\uff0c\u8fd9\u4e2a\u5185\u5b58\u6cc4\u6f0f\u662f\u4f60\u9000\u51fa\u4e86App\uff0c\u4f46\u662fwebview\u8fd8\u6ca1\u6709\u9500\u6bc1\uff0c\u6709\u4e2a\u5224\u65ad\u6807\u51c6\u662f\u5f00\u542f\u4e86webview\u8fdc\u7a0bd&hellip; <a href=\"http:\/\/cf3b5.com\/blog\/?p=134\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb <span class=\"screen-reader-text\">Webview\u7684\u5185\u5b58\u6cc4\u6f0f\u6839\u6e90\u89e3\u51b3<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"http:\/\/cf3b5.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/134"}],"collection":[{"href":"http:\/\/cf3b5.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/cf3b5.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/cf3b5.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/cf3b5.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=134"}],"version-history":[{"count":1,"href":"http:\/\/cf3b5.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/134\/revisions"}],"predecessor-version":[{"id":136,"href":"http:\/\/cf3b5.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/134\/revisions\/136"}],"wp:attachment":[{"href":"http:\/\/cf3b5.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cf3b5.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=134"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cf3b5.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}