I have an Android application that utilizes a webview. The backend server is written in Java and serves all the front-end files, creating the cache manifest through Apache Tomcat.
My predicament lies in figuring out how to make the webview load from the app cache when the server connection is unavailable, but the device still has internet access.
By manually adjusting the app code, I am able to instruct the webview to exclusively use the cache with:
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ONLY);
This ensures that my files are being properly cached. I've also attempted solutions found on stack overflow where network status is assessed and the cache mode is set accordingly based on network availability:
cm = (ConnectivityManager) this.getSystemService(Activity.CONNECTIVITY_SERVICE);
if(cm.getActiveNetworkInfo().isConnected()) ....
Nevertheless, this method fails when the device is connected online but my server is down. In such cases, it inaccurately detects a functioning network and does not resort to loading from the cache.
The ideal scenario would be for the webview to automatically switch to loading from the cache if a resource cannot be accessed over the network, similar to how a standard browser behaves. However, I'm open to alternative approaches, like having the app recognize the network connection specifically to my server and adapting the cache mode accordingly.
If anyone can suggest the most effective solution for this issue, I would greatly appreciate it. Thank you.