0

so i made a website which could be turned into mobile app simply by using webview. when the first time i install the apk, it work perfectly well but the problem is after that, the web is just static or it's not loaded anymore.

it just open up the same content like the first time i install it even though i already change the whole website. i don't know if the problem was with the webview or with the android.

i already try made a auto refresh/autoreload function in php but it only work in mobile browser and didn't work in my app... anyone know any solution?

since i'm a newbie in android developping i'll put the whole code of my main activity

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

//initializing WebView
private WebView mwebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);



    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    //WebView
    mwebView = (WebView) findViewById(R.id.myWebView);
    WebSettings webSettings = mwebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    //improve webView performance
    mwebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
    mwebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    mwebView.getSettings().setAppCacheEnabled(true);
    mwebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    webSettings.setDomStorageEnabled(true);
    webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
    webSettings.setUseWideViewPort(true);
    webSettings.setSavePassword(true);
    webSettings.setSaveFormData(true);
    webSettings.setEnableSmoothTransition(true);


    mwebView.loadUrl("http://192.168.94.2/autorefresh/");
    //force links open in webview only
    mwebView.setWebViewClient(new MyWebviewClient());

}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();


    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_home) {
        mwebView.loadUrl("http://192.168.94.2/ee1/");
    }
    if (id == R.id.nav_kolam) {
        mwebView.loadUrl("http://192.168.94.2/ee1/listkolam.php");
    }
    if (id == R.id.nav_pantai) {
        mwebView.loadUrl("http://192.168.94.2/ee1/listpantai.php");
   }
   if (id == R.id.nav_transaksi) {
       System.exit(0);
   }
   if (id == R.id.nav_about) {
       System.exit(0);

   }
    if (id == R.id.nav_keluar) {
        System.exit(0);
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}


//bikin progress dialog
private class MyWebviewClient extends WebViewClient {
    //ProgressDialogue
    @Override

    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("192.168.94.2")) {
            //open url contents in webview
            return false;
        } else {
            //here open external links in external browser or app
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }

    }
    ProgressDialog pd = null;

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        pd=new ProgressDialog(MainActivity.this);
        pd.setTitle("Mohon Tunggu Sebentar");
        pd.setMessage("Website Loading..");
        pd.show();
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        pd.dismiss();
        super.onPageFinished(view, url);
    }
}


//bikin tombol back halaman
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (mwebView.canGoBack()) {
                    mwebView.goBack();
                } else {
                    finish();
                }
                return true;
        }
    }
    return super.onKeyDown(keyCode, event);
}}

i'm using native php

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
wildape
  • 17
  • 1
  • 8
  • Hope this is what you are looking for [link](http://stackoverflow.com/questions/2465432/android-webview-completely-clear-the-cache) – fightingCoder Apr 07 '17 at 09:42
  • did removing the cache also destroying the session? since my web have login feature and i want it last long so the user wouldn't need to login again the next time they opened the app – wildape Apr 07 '17 at 12:48
  • Did you try the code? Am not sure if u can maintain the session in this case. – fightingCoder Apr 07 '17 at 16:09

3 Answers3

0

mwebView.loadUrl("about:blank"); mwebView.loadUrl("http://192.168.94.2/ee1/");

Load blank page before loading any new url. So add mwebView.loadUrl("about:blank"); before all your if-else cases.

May this help!

buzzingsilently
  • 1,546
  • 3
  • 12
  • 18
0

The webView caches all the Contents of the Site , so that it doesn't need to download images etc. For you to get the updations you need to maintain versioning your files and have a cache-control = max-age on yout index.html file

surya
  • 607
  • 5
  • 18
-1
webView.loadUrl("javascript:window.location.reload(true)"); 

This may help to reload the webview. Thanks

Fathima km
  • 2,539
  • 3
  • 18
  • 26