Xamarin Adroid: Retain Instance of Webview Content On Rotation

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.

/// <summary>
/// Handles when Main Activity needed to be refreshed e.g screen rotation
/// </summary>
protected override void OnSaveInstanceState(Bundle outState)
{
    base.OnSaveInstanceState(outState);
    // Save WebView State
    Html.wv.SaveState(outState);
}

3. Load save WebView state (if any) in OnCreate function.

protected override void OnCreate(Bundle savedInstanceState)
{
   base.OnCreate(savedInstanceState);
   SetContentView(Resource.Layout.Main);
   WebView wv = FindViewById<WebView>(Resource.Id.localWebView);

   if (savedInstanceState != null)
   {
       // Restore WebView saved state (if any)
       wv.RestoreState(savedInstanceState);
       Html.wv = wv;
       Html.context = this;
   }
   else
   {
       // Initialize WebView
       wv.SetWebChromeClient(new HybridWebChromeClient(this));
       Html.Initialize(this, wv);
       new AppLogin().Show();
   }
}

This is very simple solution to implement. Hope it helps.

Advertisements
Xamarin Adroid: Retain Instance of Webview Content On Rotation

One thought on “Xamarin Adroid: Retain Instance of Webview Content On Rotation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s