Handling orientation changes in a WebView in Android
What is a WebView?
The WebView
framework allows you to specify viewport and style properties that
make your web pages appear at the proper size and scale on all screen
configurations for all major web browsers. You can even define an interface
between your Android app and your web pages that allows JavaScript in the
web pages to call upon APIs in your app—providing Android APIs to your
web-based application.
WebView objects allow you to display web content as part of your activity
layout, but lack some of the features of fully-developed browsers. A WebView
is useful when you need increased control over the UI and advanced
configuration options that will allow you to embed web pages in a
specially-designed environment for your app.
Now it often happens that when the orientation of an app is changed by the user i.e the screen is rotated, the current progress of the WebView is lost and the Web Page reloads from the start point. This happens because whenever the orientation of the screen is changed, the onCreate() method is called and the activity is recreated . There is a simple way to deal with this issue.
·
Open the AndroidManifest.xml file of your project.
·
Inside the <activity></activity> tag, add
android:configChanges="orientation|screenSize"
·
Then in the MainActivity.java file override two methods,
onSaveInstanceState() and onRestoreInstanceState() like so…
Comments
Post a Comment