Proper Rotation with WebView

Make sure your emulator or device has auto-rotate turned on and try rotating your screen. While it does work correctly, you will notice that the WebView has to completely reload the web page. This is because WebView has too much data to save it all inside onSaveInstanceState(…). It has to start from scratch each time it is re-created on rotation.

You may think the easiest way to resolve this problem would be to retain PhotoPageFragment. However, this would not work, because WebView is part of the view hierarchy and is thus still destroyed and re-created on rotation.

For some classes like this (VideoView is another one), the Android documentation recommends that you handle certain configuration changes, such as rotation, yourself. This means that instead of the activity being destroyed on rotation, its views will just be moved around to fit the new screen size. As a result, WebView does not have to reload all of its data.

To tell PhotoPageActivity that you will handle configuration changes, make the following tweak to manifests/AndroidManifest.xml.

Listing 29.11  Handling configuration changes yourself (manifests/AndroidManifest.xml)

<manifest ... >
    ...
    <activity android:name=".PhotoPageActivity"
          android:configChanges="keyboardHidden|orientation|screenSize" />
      ...
</manifest>

This attribute says that if the configuration changes because the keyboard was opened or closed, due to an orientation change, or due to the screen size changing (which also happens when switching between portrait and landscape after Android 3.2), then you will handle the change inside the activity and it should not be destroyed. In fact, though, there is nothing special you need to do to handle the configuration change, because the views are resized and moved to fit the new screen automatically.

So that is it. Try rotating again and admire how smoothly the change is handled.

Dangers of handling configuration changes

That is so easy and works so well that you are probably wondering why you do not do this all the time. It seems like it would make life so much easier. However, handling configuration changes on your own is a dangerous habit.

First, resource qualifier-based configuration changes no longer work automatically. You instead have to manually reload your view when a configuration change is detected. This can be more complicated than it sounds.

Second, and more important, this approach will likely cause you to not bother with overriding Activity.onSavedInstanceState(…) to stash transient UI states. Doing so is still necessary, even if the activity is retained across rotation, because you still have to worry about death and re-creation in low-memory situations. (Remember, the activity can be destroyed and stashed by the system at any time if it is not in the running state, as shown in Figure 4.9 .)

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
34.201.8.144