RestorePicture WebView Method - Prevent Web Browsing for Page Reload

As I said in this thread, I want my WebView to not allow a web page to reload when another action comes to the fore, or just when the orientation changes. The reason is that WebView content is generated almost entirely by Javascript / AJAX. After searching several forums, I found out that many people suggested using the "saveState" and "restoreState" methods, but when I look at the documentation, it says:

Note that this method no longer restores display data for this WebView. See SavePicture (Bundle, File) and restorePicture (Bundle, File) for saving and restoring displayed data.

So here I used this savePicture and restorePicture as follows:

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); 

... some other lines ....

  setContentView(R.layout.main); mWebView = (WebView) findViewById(R.id.webview); if (savedInstanceState == null){ loadInicialWebUi(); }else{ mWebView.restoreState(savedInstanceState); boolean restoredPic = mWebView.restorePicture(savedInstanceState, new File("savedDisplayWebView.temp")); Log.d(TAG, "restored Picture:" + restoredPic); } } @Override protected void onSaveInstanceState(Bundle savedInstanceState) { mWebView.saveState(savedInstanceState); boolean savedPic = mWebView.savePicture(savedInstanceState, "savedDisplayWebView.temp"); Log.d(TAG, "saved Picture:" + savedPic); super.onSaveInstanceState(savedInstanceState); } 

And well, these magazines showed that he was saving the image, but he could not restore it. I suspect there might be something in the file links, but I could not think of a better way to get a link to the file I created while maintaining the state.

Is anyone thrilled? I would appreciate any tips / suggestions. Thanks in advance.

Manuel

+6
android webview android-webview
source share
1 answer

I would load the page in onCreate() and put this attribute in my activity in the manifest:

 android:configChanges="orientation|keyboardHidden" 

This will prevent the Activity from restarting when the orientation changes. And since your activity loads the page in onCreate() , moving another action to the foreground will not cause the page to reload.

0
source share

All Articles