Question

I have a WebView with data populated from local javascript (in the "asset" folder). The WebView takes several seconds to load so I'd like to keep it on device rotation. I don't want to use configChanges since it has some other collateral effects.

It seems to me that the best approach is to make a Picture from the WebView and load the Picture instead of the WebView on rotation but I can't figure out how.

Here is the code I tried:

Canvas canvas = new Canvas();
pictureNightDay.draw(canvas);
Bitmap b = Bitmap.createBitmap(pictureNightDay.getWidth(), pictureNightDay.getHeight(),Config.ARGB_8888);
myWebViewNightDay.draw(new Canvas(b));

but this only shows a blank View.

The "pictureNightDay" was previously saved with

pictureNightDay = myWebViewNightDay.capturePicture();

Thanks, bye Maurizio

Was it helpful?

Solution 2

At last I came with the following answer: I used two Views, one WebView and one ImageView (initially hidden)

Save the picture when WebView is loaded:

pictureNightDay = myWebViewNightDay.capturePicture();

(pictureNightDay is a static field).

Then on rotation check if pictureNightDay is not null and swap the ImageView and WebView visibility:

ImageView im = (ImageView) l.findViewById(R.id.tripreport_tipology_imagenightday);
im.setImageDrawable(new PictureDrawable(pictureNightDay));
im.setVisibility(View.VISIBILE);
myWebViewNightDay.setVisibility(View.GONE);

OTHER TIPS

From the Developer Docs (under 'Cookies and Window Management'):

The standard behavior for an Activity is to be destroyed and recreated when the device orientation or any other configuration changes. This will cause the WebView to reload the current page. If you don't want that, you can set your Activity to handle the orientation and keyboardHidden changes, and then just leave the WebView alone. It'll automatically re-orient itself as appropriate. Read Handling Runtime Changes for more information about how to handle configuration changes during runtime.

Esentially if you tell the system what you want to keep during these changes, it will preserve it for you. I have to say this is a great system. Everytime I think I have to do something the hard way, a little bit of searching finds me way to have the system do it for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top