在WebView中加载上次查看的页面

时间:2014-12-12 14:48:23

标签: java android webview

我有一个加载特定URL的WebView。如果用户从Web视图导航到应用程序的另一部分或者他们离开应用程序,我希望Web视图加载他们离开时所在的最后一页。在iOS中,我可以通过获取当前URL并将其保存到NSUserDefaults来实现此目的,但我不确定如何在Android上完成此操作。

这是我的代码。

public class MyKYFB extends Fragment {

    private WebView web;
    private ProgressBar progressBar;

    private String url;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View rootView = inflater.inflate(R.layout.fragment_my_kyfb, null);

        url = "https://my.kyfb.com";

        web = (WebView)rootView.findViewById(R.id.kyfb_web);
        progressBar = (ProgressBar)rootView.findViewById(R.id.prgPageLoading);

        web.getSettings().setDomStorageEnabled(true);
        web.getSettings().setJavaScriptEnabled(true);
        web.getSettings().setSupportZoom(true);
        web.getSettings().setBuiltInZoomControls(true);
        web.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        web.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        web.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);

        web.loadUrl(url);

        web.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView webView, int progress) {
                getActivity().setProgress(progress * 100);
                progressBar.setProgress(progress);
            }
        });

        web.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(web, url, favicon);
                progressBar.setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(web, url);
                progressBar.setProgress(0);
                progressBar.setVisibility(View.GONE);
            }

            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(getActivity(), description, Toast.LENGTH_SHORT).show();
            }

            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                if(url.endsWith(".mp4") || url.endsWith(".3gp") || url.endsWith(".avi") || url.endsWith(".flv")){
                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(url));
                    startActivity(i); //warning no error handling will cause force close if no media player on phone.
                    return true;
                }

                view.loadUrl(url);
                return true;
            }
        });

        return rootView;
    }
}

2 个答案:

答案 0 :(得分:1)

如果您想在用户重新打开应用时打开相同的网址,则必须将其保存在某处。一种简单的方法是使用SharePreferences:

当用户离开应用/ WebView时:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(SP_LAST_WEBVIEW_URL), getString(yourWebView.getUrl());
editor.commit();

将它放在当前班级的开头:private static final String SP_LAST_WEBVIEW_URL = "com.app.yourapp.LASTWEBVIEWURL";

加载已保存的网址:将web.loadUrl(url);替换为:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
String savedUrl = getResources().getString(SP_LAST_WEBVIEW_URL);
web.loadUrl(savedUrl);

答案 1 :(得分:1)

在onPageFinished中,将URL存储到系统首选项(或其他位置)。

然后,在onCreateView中,检索此值,如果不为null,则将其用作要加载的URL。