Android WebView在活动中重用

时间:2015-08-14 01:15:08

标签: android webview garbage-collection

我想在Activity中多次重用WebView。

当我多次调用WebView.loadData()时,内存的使用会增加。

在我获得铬OOM之前,我看不到任何GC。

(我传递给loadData()的HTML代码正在加载图片。像这样,img src = blabla ..)

我已经搜索了很多建议来清除WebView,但它们对我的应用程序没有帮助。

我的应用程序执行GC的唯一方法是,当我完成WebView活动时。

有没有办法在没有完成活动的情况下清除或破坏WebView?

下面是我的完整源代码。

public class ViewActivity extends AppCompatActivity {


private static final String tag = "CCTEST_VIEW";
private ListView lvList;
private FrameLayout list_header;
private ArrayAdapter adapter;
private ArrayList<String> arrayList;
private ArrayList<String> contents;
private WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    lvList = (ListView) findViewById(R.id.lvList);
    list_header = (FrameLayout) findViewById(R.id.main);
    arrayList = new ArrayList<String>();
    adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);

    lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            int pos = position - lvList.getHeaderViewsCount();
            launchWebView(contents.get(pos));
        }
    });

    lvList.setAdapter(adapter);


    SharedPreferences prefs = getApplicationContext().getSharedPreferences("prefs", 0);
    String data = prefs.getString("data", null);
    Type type = new TypeToken<List<String>>(){}.getType();
    contents = new Gson().fromJson(data, type);

    for (int i = 0; i < contents.size(); i++) {
        arrayList.add("a." + i);
    }

}

private void launchWebView(String body) {
    genWebView();
    webView.loadData(body, "text/html", "UTF-8");

}


private void clearWebView() {
    if (list_header.getChildCount() == 0) return;


    webView.stopLoading();
    webView.loadUrl("about:blank");
    webView.clearHistory();
    webView.clearCache(true);
    webView.pauseTimers();
    webView.removeAllViews();
    webView.setWebChromeClient();
    webView.destroy();
    webView = null;

    list_header.removeAllViews();

}

private void genWebView() {
    clearWebView();
    if (webView == null) {

        webView = new WebView(this);

        list_header.addView(webView);
    }
    else {
        webView.onResume();
    }
}






@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

0 个答案:

没有答案