在我的应用程序中我有一个滑动菜单,在滑动菜单中我有四个选项,分别是水果,蔬菜,动物,鸟类。这些都是碎片。在每个片段中,我都有一个Web视图。现在我想要的是当我加载网页并执行一些搜索时,现在我将打开另一个片段,并希望执行另一个搜索。它很好。但我想要的是如果我正在执行一些搜索,那么它必须保存我访问过的最后一个URL。我可以在单个网络中保存最后一个网址但无法在多个网页中保存。 这是我的代码,我可以用单个类保存最后一个URL。
public class SearchFragment extends Fragment {
WebView wv;
private ProgressBar bar;
private String url="http://www.google.com";
public SearchFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_search, container,
false);
wv = (WebView)rootView.findViewById(R.id.search_web);
bar = (ProgressBar)rootView.findViewById(R.id.search_bar);
bar.setVisibility(View.VISIBLE);
loadUrl();
return rootView;
}
private void loadUrl() {
WebSettings webSettings = wv.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
/* below function saved the password in WebView */
webSettings.setSavePassword(true);
wv.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int progress) {
if (progress == 100) {
bar.setVisibility(View.GONE);
wv.setVisibility(View.VISIBLE);
}
}
});
wv.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
//Toast.makeText(CommunityFragment.this, description,
Toast.LENGTH_SHORT).show();
Toast.makeText(getActivity(), description,
Toast.LENGTH_LONG).show();
}
});
wv.loadUrl(url);
}
// flipscreen not loading again
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
// Handling BackPress events
public void onBackPressed() {
if (wv.canGoBack()) {
wv.goBack();
} else {
getActivity().finish();
}
}
}