Reputation: 312
I'm creating an app that loads my website to a webview. I want to prevent that page reloads when I came back to app. If I change to other app or power off and came back the page shows without refresh, the problem occurs only it stays off for a few minutes. I think that the application change the state to stop or is destroyed and when I enter again is called the method oncreate.
I've tried to change android:launchMode to singleTop an singleTask without success, is the creation of a service the solution? Thanks in advance.
Upvotes: 1
Views: 841
Reputation: 4617
In order to prevent refreshing of WebView
, you need to save and restore state.
This is done by overriding lifecycle methods onSaveInstanceState
and onRestoreInstanceState
@Override
protected void onSaveInstanceState(Bundle outBundle)
{
super.onSaveInstanceState(outBundle);
webView.saveState(outBundle);
}
@Override
protected void onRestoreInstanceState(Bundle savedBundle)
{
super.onRestoreInstanceState(savedBundle);
webView.restoreState(savedBundle);
}
Furthermore, you can try to set cache on the WebView
instance.
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
webView.loadUrl(url);
Upvotes: 1