每次应用程序启动时如何清除webview的数据?

时间:2022-04-25 06:30:40

i have an application where i load url in a webview .But when i exit from the app then launches the app again it shows the same page .I want everytime the app launches it should started from the main page.How can i do this? Here is my oncreatemethod:

我有一个应用程序,我在网页视图中加载网址。但是当我退出应用程序然后再次启动应用程序它显示相同的页面。我希望每次应用程序启动它应该从主页面开始。我怎么能这样做?这是我的oncreate方法:

    web = (WebView) findViewById(R.id.webview01);
    progressBar = (ProgressBar) findViewById(R.id.progressBar1);




    web.getSettings().setLoadWithOverviewMode(true);
    web.getSettings().setUseWideViewPort(true);

    web.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    web.getSettings().setPluginState(PluginState.ON);
    web.getSettings().setJavaScriptEnabled(true); 
    web.setWebViewClient(new myWebClient());

here is mywebclient class:

这是mywebclient类:

  public class myWebClient extends WebViewClient {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub

            super.onPageStarted(view, url, favicon);

        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub
             if(url.startsWith("http://www.amazon.com/")) { 

                 Intent i = new Intent(Intent.ACTION_VIEW);
                 i.setData(Uri.parse(url));
                 startActivity(i);
                } 

             else{

        view.loadUrl(url);

             }
            return true;




        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub


            super.onPageFinished(view, url);

            view.setVisibility(View.VISIBLE);
            progressBar.setVisibility(View.GONE);

        }
    }

And here is onresume():

这是onresume():

@Override
    protected void onResume() {

        super.onResume();
        web.setVisibility(View.INVISIBLE);
        web.loadUrl("http://savebigshopper.com/maybelline");

    }

Please help

1 个解决方案

#1


0  

The following lines of code that are above where are they located in your activity?

以下代码行位于您的活动中的哪个位置?

web = (WebView) findViewById(R.id.webview01);

//...

web.loadUrl("http://savebigshopper.com/maybelline");

My guess would be onCreate method. If that is the cause you should move them into the onResume method.

我的猜测是onCreate方法。如果这是原因,您应该将它们移动到onResume方法中。

EDIT: really looking at it again the only code that really needs to be move to onResume is this line:

编辑:真的再次看到它真正需要移动到onResume的唯一代码是这一行:

web.loadUrl("http://savebigshopper.com/maybelline");

EDIT2:

"Everything is working fine but when i again launch the app 1st show the previous page then it shows the main page"

“一切都工作正常,但当我再次启动应用程序时,显示上一页,然后显示主页面”

That would make sense to me as this will act like a web browser. You could possible hide the webview until it is done loading.

这对我来说很有意义,因为它会像网络浏览器一样。你可以隐藏webview,直到它完成加载。

// add this line to your onPageFinished method inside your WebViewClient

view.setVisibility(View.VISIBLE); //Show view when page has loaded.

// inside onResume method

web.setVisibility(View.INVISIBLE); //Hide it first
web.loadUrl("http://savebigshopper.com/maybelline");

#1


0  

The following lines of code that are above where are they located in your activity?

以下代码行位于您的活动中的哪个位置?

web = (WebView) findViewById(R.id.webview01);

//...

web.loadUrl("http://savebigshopper.com/maybelline");

My guess would be onCreate method. If that is the cause you should move them into the onResume method.

我的猜测是onCreate方法。如果这是原因,您应该将它们移动到onResume方法中。

EDIT: really looking at it again the only code that really needs to be move to onResume is this line:

编辑:真的再次看到它真正需要移动到onResume的唯一代码是这一行:

web.loadUrl("http://savebigshopper.com/maybelline");

EDIT2:

"Everything is working fine but when i again launch the app 1st show the previous page then it shows the main page"

“一切都工作正常,但当我再次启动应用程序时,显示上一页,然后显示主页面”

That would make sense to me as this will act like a web browser. You could possible hide the webview until it is done loading.

这对我来说很有意义,因为它会像网络浏览器一样。你可以隐藏webview,直到它完成加载。

// add this line to your onPageFinished method inside your WebViewClient

view.setVisibility(View.VISIBLE); //Show view when page has loaded.

// inside onResume method

web.setVisibility(View.INVISIBLE); //Hide it first
web.loadUrl("http://savebigshopper.com/maybelline");