In my Android app I need to cache the (already loaded) content of WebView on pause and then restore it on resume. I wonder how can I do this?
It is cached automatically unless you disable. Nothing special needs to be done.
A common problem is that badly designed parent resets the view itself in methods like onStart or onResume, forcing to reload the contents. You need to be careful with the view in these methods, it may already with content.
Related
I have a webview which loads content from URL. However, the webview reloads the content on screen rotation, which I would like to avoid.
I have checked similar questions on SO which all recommended either:
Putting orientation into android:configChanges="orientation..." which is generally discouraged and I don't want to use it
Or saving the webview's state with webView.saveState(outState)
As for the 2nd option, the documentation now states:
Please note that this method no longer stores the display data for this WebView. The previous behavior could potentially leak files if restoreState(Bundle) was never called.
Which means that it's not the solution either. Is it possible to solve this?
So, the thing is that, if I am in a webview on the app and then tap another option of the menu (also in webview) and then go back to the first option this one will refresh the content, I want to avoid that, because if a put a login screen and the user taps another option and goes back they will have the login screen again even if they are already logged.
I already tried this answer but didn't work.
I suspect the problem is that the WebView is not wired into the lifecycle of your activity/fragment and can not distinguish between the first time it is loaded and subsequent times it is loaded. Additionally if the WebView is destroyed (highly probable) the WebView must reload the data.
Ensure that you are saving and restoring the state of your Activity/Fragment correctly and keep track of which webviews are loaded (you can use a HashMap, or boolean or whatever you like). https://developer.android.com/guide/components/activities/activity-lifecycle.html#saras
When you go to load your webviews consult the data from step 1 to determine if you need to reload the content. You may need to pass some data to the HTML to indicate the state (first load etc).
When app is running, users adds some views in the Layout. I want to store this Layout, so that when i reopen the app it should return the same state.
How can I achieve that?
Use onSaveInstanceState() and onRestoreInstanceState() callbacks to save and restore your views.
More at Managing the Activity Lifecycle and Recreating an Activity.
[EDIT] - As per #kadatt comment.
I think what you are trying to do is possible duplicate of android-saving-state-of-dynamically-changed-layout.
I dont think there is any standard way of doing this. You will have to implement your own functionality to save state of dynamic UI in some form which can be loaded again.
i am new to android.i have one single activity with main.xml file. Now, i have one scroll view in that main.xml file.when i run my application in portrait mode and when i go to the bottom of the scroll view and than when i change my application state to the landscape mode than i go to the top of the scroll view..that means when we switch between portrait to landscape or vice versa the activity is recalled. so need to save the application state in portrait and restore in landscape. so any solution of it?
thanks in advance
Aamirkhan I.
The documentation does a decent job of explaining different ways to handle configuration changes, including screen orientation changes. One of those methods, which is good for saving temporary state of the UI, is saving data in the onSavedInstanceState() method--as #Jason Kuang mentioned.
Generally, you can rely on Android to save and restore the state of Views without any special effort on your part. The source code for the protected method onSaveInstanceState() explains (emphasis added):
The default implementation takes care of most of the UI per-instance state for you by calling android.view.View.onSaveInstanceState() on each view in the hierarchy that has an id, and by saving the id of the currently focused view (all of which is restored by the default implementation of onRestoreInstanceState(android.os.Bundle)). If you override this method to save additional information not captured by each individual view, you will likely want to call through to the default implementation, otherwise be prepared to save all of the state of each view yourself.
This is a little deceptive, because the API documentation states that EditTexts and TextViews must have android:freezesText="true" explicitly declared on them in your layout XML files to ensure that Android automatically stores their state when onSaveInstanceState() is invoked. I have not tested this recently, but it is what the source code seems to be doing. Therefore, handling temporary UI state on your own is best.
Another tip: You can explicitly prevent the storage of temporary data for a View by calling setSaveEnabled(false) on that View. (This will not affect its children.)
As a rule, it's a good idea to manually save the on-screen state in your onPause() method, and also in onSaveInstanceState().
My understanding is that WebView's PictureListener.onNewPicture() is called whenever the already loaded page finished rendering (or re-rendering in case certain events make the WebKit engine re-calculate page layout).
I also (think that I) understand that those WebView renders are controlled internally in WebView and ordinarily I wouldn't care how they are being generated.
However, for a certain function in my app, I need to trigger an extra PictureListener.onNewPicture() without re-loading the url (i.e. no reload() or loadUrl()).
Essentially, what I am looking for is a function like repaint() or redraw() or some other mechanism that only generate one more PictureListener.onNewPicture() and that's it (without the slowness and overhead associated with re-loading data).
Is there a way to achieve this?
Try using the requestLayout() function.
not sure if it will do what you want, but it worth a try.