Bladeren bron

webview out of memory

hanjing 6 jaren geleden
bovenliggende
commit
937a330d6a

+ 20 - 7
app/src/main/java/com/sheep/gamegroup/module/webview/fragment/FgtWeb.java

@@ -23,6 +23,7 @@ import android.webkit.WebChromeClient;
 import android.webkit.WebSettings;
 import android.webkit.WebView;
 import android.webkit.WebViewClient;
+import android.widget.FrameLayout;
 import android.widget.ImageView;
 import android.widget.ProgressBar;
 
@@ -73,7 +74,8 @@ import static android.app.Activity.RESULT_OK;
  */
 public class FgtWeb extends BaseFgtWeb {
 
-    @BindView(R.id.webview)
+    @BindView(R.id.web_container)
+    FrameLayout webContainer;
     WebView mWebView;
     @BindView(R.id.act_web_loading_pb)
     ProgressBar act_web_loading_pb;
@@ -211,15 +213,26 @@ public class FgtWeb extends BaseFgtWeb {
                 e.printStackTrace();
             }
         }
+        super.onDestroy();
+    }
+
+    public void onDetach() {
         if (mWebView != null) {
-            if (mWebView.getParent() != null) {
-                ((ViewGroup) mWebView.getParent()).removeView(mWebView);
+            try {
+                if (mWebView.getParent() != null) {
+                    ((ViewGroup) mWebView.getParent()).removeView(mWebView);
+                }
+                mWebView.setVisibility(View.GONE);
+                mWebView.removeAllViews();
+                mWebView.loadUrl("about:blank");
+                mWebView.freeMemory();
+                mWebView.destroy();
+            } catch (IllegalArgumentException e) {
+                LogUtil.logI(e.getMessage());
             }
-            mWebView.setVisibility(View.GONE);
-            mWebView.removeAllViews();
-            mWebView.destroy();
+            mWebView = null;
         }
-        super.onDestroy();
+        super.onDetach();
     }
 
     /**

+ 23 - 7
app/src/main/java/com/sheep/gamegroup/module/webview/fragment/FgtWebX5.java

@@ -6,6 +6,7 @@ import android.content.Intent;
 import android.graphics.Bitmap;
 import android.net.Uri;
 import android.os.Build;
+import android.os.Bundle;
 import android.os.Environment;
 import android.provider.MediaStore;
 import android.support.v7.app.ActionBar;
@@ -17,6 +18,7 @@ import android.view.View;
 import android.view.ViewGroup;
 import android.view.animation.Animation;
 import android.view.animation.AnimationUtils;
+import android.widget.FrameLayout;
 import android.widget.ImageView;
 import android.widget.ProgressBar;
 
@@ -77,7 +79,8 @@ import static android.app.Activity.RESULT_OK;
  */
 public class FgtWebX5 extends BaseFgtWebX5 {
 
-    @BindView(R.id.webview)
+    @BindView(R.id.web_container)
+    FrameLayout webContainer;
     WebView mWebView;
     @BindView(R.id.act_web_loading_pb)
     ProgressBar act_web_loading_pb;
@@ -99,6 +102,8 @@ public class FgtWebX5 extends BaseFgtWebX5 {
     }
 
     public void initView() {
+        mWebView = new WebView(SheepApp.getInstance());
+        webContainer.addView(mWebView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
         ViewUtil.setImage(act_web_loading_iv, R.drawable.gif_sheep_loading);
 
         if (webParams == null) {
@@ -200,15 +205,26 @@ public class FgtWebX5 extends BaseFgtWebX5 {
                 e.printStackTrace();
             }
         }
+        super.onDestroy();
+    }
+
+    public void onDetach() {
         if (mWebView != null) {
-            if (mWebView.getParent() != null) {
-                ((ViewGroup) mWebView.getParent()).removeView(mWebView);
+            try {
+                if (mWebView.getParent() != null) {
+                    ((ViewGroup) mWebView.getParent()).removeView(mWebView);
+                }
+                mWebView.setVisibility(View.GONE);
+                mWebView.removeAllViews();
+                mWebView.loadUrl("about:blank");
+                mWebView.freeMemory();
+                mWebView.destroy();
+            } catch (IllegalArgumentException e) {
+                LogUtil.logI(e.getMessage());
             }
-            mWebView.setVisibility(View.GONE);
-            mWebView.removeAllViews();
-            mWebView.destroy();
+            mWebView = null;
         }
-        super.onDestroy();
+        super.onDetach();
     }
 
     /**

+ 6 - 5
app/src/main/res/layout/fgt_web.xml

@@ -4,12 +4,13 @@
     android:layout_height="match_parent"
     android:background="@color/white_F9F9F9">
 
-    <WebView
-        android:id="@+id/webview"
-        android:layout_width="match_parent"
-        android:layout_height="match_parent"
+    <FrameLayout
+        android:id="@+id/web_container"
         android:background="@drawable/x_shap_shadow_bg_rectgangle_white"
-        android:scrollbars="none" />
+        android:layout_width="match_parent"
+        android:layout_height="match_parent">
+
+    </FrameLayout>
 
     <ProgressBar
         android:id="@+id/act_web_loading_pb"

+ 6 - 5
app/src/main/res/layout/fgt_web_x5.xml

@@ -4,12 +4,13 @@
     android:layout_height="match_parent"
     android:background="@color/white_F9F9F9">
 
-    <com.tencent.smtt.sdk.WebView
-        android:id="@+id/webview"
-        android:layout_width="match_parent"
-        android:layout_height="match_parent"
+    <FrameLayout
+        android:id="@+id/web_container"
         android:background="@drawable/x_shap_shadow_bg_rectgangle_white"
-        android:scrollbars="none" />
+        android:layout_width="match_parent"
+        android:layout_height="match_parent">
+
+    </FrameLayout>
 
     <ProgressBar
         android:id="@+id/act_web_loading_pb"