下面列出了android.webkit.WebView#destroy ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void destroyWebView(WebView mWebView) {
if (mWebView != null) {
try {
ViewParent parent = mWebView.getParent();
if (parent != null) {
((ViewGroup) parent).removeView(mWebView);
}
mWebView.stopLoading();
mWebView.getSettings().setJavaScriptEnabled(false);
mWebView.clearHistory();
mWebView.clearView();
mWebView.removeAllViews();
mWebView.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
}
static final void clearWebView(WebView m) {
if (m == null) {
return;
}
if (Looper.myLooper() != Looper.getMainLooper()) {
return;
}
m.loadUrl("about:blank");
m.stopLoading();
if (m.getHandler() != null) {
m.getHandler().removeCallbacksAndMessages(null);
}
m.removeAllViews();
ViewGroup mViewGroup = null;
if ((mViewGroup = ((ViewGroup) m.getParent())) != null) {
mViewGroup.removeView(m);
}
m.setWebChromeClient(null);
m.setWebViewClient(null);
m.setTag(null);
m.clearHistory();
m.destroy();
m = null;
}
private void clearCache() {
WebView webView = new WebView(mActivity);
webView.clearCache(true);
webView.destroy();
Utils.showSnackbar(mActivity, R.string.message_cache_cleared);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final String userAgent = PreferenceManager.getDefaultSharedPreferences(getActivity()).getString(getString(R.string.user_agent_pref), null);
if (userAgent != null) {
RequestQueueUtil.getInstance().setUserAgent(userAgent);
} else {
try {
final WebView webView = new WebView(getActivity());
final String webViewAgent = webView.getSettings().getUserAgentString();
PreferenceManager.getDefaultSharedPreferences(getActivity()).edit().putString(getString(R.string.user_agent_pref), webViewAgent).apply();
webView.destroy();
} catch (Exception e) {
if (PreferenceManager.getDefaultSharedPreferences(getActivity()).getString(getString(R.string.user_agent_pref), null) == null) {
final String defaultAgent = System.getProperty("http.agent");
PreferenceManager.getDefaultSharedPreferences(getActivity()).edit().putString(getString(R.string.user_agent_pref), defaultAgent).apply();
}
}
}
if (getActivity() instanceof MimiActivity) {
toolbar = ((MimiActivity) getActivity()).getToolbar();
}
rootView = inflater.inflate(R.layout.fragment_boards_list, container, false);
errorSwitcher = rootView.findViewById(R.id.error_switcher);
boardListAdapter = new BoardListAdapter(getActivity(), new ArrayList<>());
final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
boardsList = rootView.findViewById(R.id.boards_list);
boardsList.addItemDecoration(new DividerItemDecoration(boardsList.getContext(), RecyclerView.VERTICAL));
boardsList.setLayoutManager(layoutManager);
boardsList.setAdapter(boardListAdapter);
boardListAdapter.setDragListener(viewHolder -> itemTouchHelper.startDrag(viewHolder));
boardListAdapter.setOnItemLongClickListener((parent, view, position, id) -> {
if (getActivity() != null) {
getActivity().startActionMode(getActionMode());
}
return true;
});
return rootView;
}
private void countDownAndDestroy(WebView view) {
latch.countDown();
view.destroy();
}
private void clearCache() {
WebView webView = new WebView(mActivity);
webView.clearCache(true);
webView.destroy();
Utils.showSnackbar(mActivity, R.string.message_cache_cleared);
}