2

My app consists of a mainactivity, which leads to a second activity with the option of 1 of 3 buttons that all lead to different webviews. The webviews just load simple HTML pages with a photo in it and some text.

I want my users to load the page 1 time and then cache the page. i used web.setCacheMode(LOAD_CACHE_ELSE_NETWORK) for this. all fine and dandy, but here's the catch. I use a Refresh button in the action bar. wich just calls web.reload() or web.loadUrl("MyURL") But, when i use the above cache mode the refresh button doesnt work, it just loads the cached page. So when i update my HTML page with a new photo or text it doesnt get loaded in. But when i use web.setcacheMode(LOAD_DEFAULT) it doesnt get cached but the refresh button works. i determined i NEED to have a internet connecting for the cache to work.... wich is quite weird...

below is the code i use in the webview.

public class AGF_R1 extends ActionBarActivity {


    WebView web;

    private int getScale(){
        Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        int width = display.getWidth();
        Double val = new Double(width)/new Double(1200);
        val = val * 100d;
        return val.intValue();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater mif = getMenuInflater();
        mif.inflate(R.menu.menu_agf__r1, menu);
        return super.onCreateOptionsMenu(menu);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //regelt de onClick
        switch (item.getItemId()) {
            case R.id.herlaad:
                web.reload();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(com.KnapperDev.knapper.jrw.R.layout.activity_agf__r1);

        web = (WebView) findViewById(com.KnapperDev.knapper.jrw.R.id.AGF_WebView1);
        web.setInitialScale(getScale());
        web.getSettings().setJavaScriptEnabled(true);
        web.getSettings().setBuiltInZoomControls(true);
        web.getSettings().setLoadsImagesAutomatically(true);
        web.loadUrl("http://77.161.45.179/AGF/AGF_R1.html");
        web.getSettings().setAppCacheEnabled(true);
        web.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        web.setWebViewClient(new AGF_WebClient());

        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY);

        int week = cal.get(Calendar.WEEK_OF_YEAR);
        int w1 = week;
        int w2 = week + 1;
        int w3 = week + 2;
        String rst = "Rooster week: ";

        getSupportActionBar().setTitle(rst + w1);

    }

    public class AGF_WebClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()){
            web.goBack();
            return  true;
        }
        return super.onKeyDown(keyCode, event);
    }

}

1 Answer 1

2

Instead of refreshing the webpage, you could try flushing the cache with the refresh button?

Bit of a brute force solution admittedly...

Reference: http://developer.android.com/reference/android/webkit/WebView.html#clearCache%28boolean%29

Sign up to request clarification or add additional context in comments.

1 Comment

it's a good idea, except the part where it whipes the whole cache, and it doesn't mention anything about only clearing a specific webview. I assume this isn't possible?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.