I’m developing an android hybrid app using C# in VS 2013. The app make use of WebView which was giving me problem with rotation. Every time the screen rotate from landscape to portrait the app will auto refresh to back login page. After googling a bit, I found that when screen orientation change android’s activity will refresh. Hence, I need to save the instance of WebView when the activity about to get refresh on screen orientation change and reload the save WebView when the activity reinitialize.
This is the URL to source of the solution: http://twigstechtips.blogspot.com/2013/08/android-retain-instance-of-webview.html
The URL above gave the fix in Java. Below is my converted version to C# when using Xamarin.
1. Add ConfigurationChanges setting to the activity. In Solution above, it was added to the manifest xml however, I find that in Xamarin C# you can just add it in to the Activity tag.
[Activity(Label = Global.Config.MainLabel, MainLauncher = true, Icon = "@drawable/icon", Theme = Global.Config.MainTheme, ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.KeyboardHidden | Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize)]
public class MainActivity : Activity
2. Override Activity.onSaveInstanceState() and save the state of the webview in this method. FYI, I stored my WebView in Html class.
/// Handles when Main Activity needed to be refreshed e.g screen rotation
protected override void OnSaveInstanceState(Bundle outState)
// Save WebView State
3. Load save WebView state (if any) in OnCreate function.
protected override void OnCreate(Bundle savedInstanceState)
WebView wv = FindViewById<WebView>(Resource.Id.localWebView);
if (savedInstanceState != null)
// Restore WebView saved state (if any)
Html.wv = wv;
Html.context = this;
// Initialize WebView
This is very simple solution to implement. Hope it helps.