Quantcast

Webview problem with screen rotation

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Webview problem with screen rotation

simpleweb
I'm fairly new to Mono for Android. I'm building an app that has a webview. The webview loses content when screen orientation changes. I have tried several things that were suggested, but not working.  I have tried these:

http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change/

http://stackoverflow.com/questions/9766584/android-webview-content-should-not-be-lost-on-orientation-change-but-gui-should

none worked. It loses the content. Please advise
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Webview problem with screen rotation

Terry151151
The only way I've found to do this and has worked for me is to handle Configuration changes yourself.
On rotation remove the WebView from it's container (Usually a LinearLayout), do the base configuration call, then reattach the WebView to it container.

(Summary from my own code):

[Activity( ConfigurationChanges = ConfigChanges.Keyboard | ConfigChanges.KeyboardHidden | ConfigChanges.Orientation )]
         class BaseWebView : Activity
         {
        private WebView Browser;

        protected override void OnSaveInstanceState( Bundle outState )
                {
                        base.OnSaveInstanceState( outState );
                        Browser.SaveState( outState );
                }

                protected override void OnRestoreInstanceState( Bundle savedInstanceState )
                {
                        base.OnRestoreInstanceState( savedInstanceState );
                        if( savedInstanceState != null )
                                Browser.RestoreState( savedInstanceState );
                }

                public override void OnConfigurationChanged( Android.Content.Res.Configuration newConfig )
                {
                        WebViewContainer.RemoveView( Browser );
                        base.OnConfigurationChanged( newConfig );
                        WebViewContainer.AddView( Browser );
                }
       }

    Hope this helps.
    Terry.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Webview problem with screen rotation

Jonathan Pryor-2
In reply to this post by simpleweb
On Aug 4, 2012, at 2:54 PM, simpleweb <[hidden email]> wrote:
> 'm building an app that has a webview. The webview loses content when screen orientation changes.

The reason for this is that, by default, whenever the configuration changes a new Activity is created. Rotate the screen, and the current Activity is destroyed and a new one is created.

> I have tried several things that were suggested, but not working.  I have tried these:

Can you elaborate on how they are not working? Are there any error messages/etc.?

> http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change/

This solves the issue by overriding the Activity.OnSaveInstanceState() and Activity.OnRestoreInstanceState methods:

        http://androidapi.xamarin.com/?link=M:Android.App.Activity.OnSaveInstanceState
        http://androidapi.xamarin.com/?link=M:Android.App.Activity.OnRestoreInstanceState

This works by asking the WebView to save it's information (URL, screen position) and then restoring that information within the new Activity/WebView.

However, in order for this to work you need to override the appropriate Activity methods. If you keep the same Java names, you won't actually be overriding them:

        partial class MyActivity {
                // BROKEN
                protected void onSaveInstanceState(Bundle outState) {...}

                // BROKEN
                protected void onRestoreInstanceState(Bundle savedInstanceState) {...}
        }

You need to use the `override` keyword and properly capitalize the method names:

        partial class MyActivity {
                protected override void OnSaveInstanceState(Bundle outState) {...}

                protected override void OnRestoreInstanceState(Bundle savedInstanceState) {...}
        }

> http://stackoverflow.com/questions/9766584/android-webview-content-should-not-be-lost-on-orientation-change-but-gui-should

This works by disabling configuration changes altogether, thus ensuring that your Activity instance isn't destroyed. This _should_ work, but...how are you specifying this?

DO NOT edit Properties\AndroidManifest.xml to set XML information for C# types; it's very tricky. You should instead use custom attributes:

        [Activity(MainLauncher=true, ConfigurationChanges=ConfigChanges.Orientation)]
        public partial class MyActivity : Activity {
                // ...
        }

 - Jon

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Webview problem with screen rotation

simpleweb
Thanks for the info guys. I found that the problem is an image button. When screen rotates, I get a null exception. When I remove the image button I don't. I have a FindViewByID call in OnCreate(Bundle bundle)

see below:

// this return null exception when screen orientation changes.
var hm= FindViewById<ImageButton>(Resource.Id.ibHomeFromLink);


The error is:

Unhandled Exception:

System.NullReferenceException: Object reference not set to an instance of an object


thanks for your help.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Webview problem with screen rotation

Jonathan Pryor-2
On Aug 7, 2012, at 12:13 AM, simpleweb <[hidden email]> wrote:
> Thanks for the info guys. I found that the problem is an image button. When screen rotates, I get a null exception.

Stack trace?

> When I remove the image button I don't. I have a FindViewByID call in OnCreate(Bundle bundle)
>
> see below:
>
> // this return null exception when screen orientation changes.
> var hm= FindViewById<ImageButton>(Resource.Id.ibHomeFromLink);

So `hm` is null, and then you later try to use `hm` which results in the NullReferencException, right?

I suspect that you have multiple layouts:

        http://developer.android.com/guide/practices/screens_support.html#qualifiers

For example, you may have Resources\Layout\Main.axml and Resources\Layout-land\Main.axml files. When you rotate to landscape orientation, then Resources\Layout-land\Main.axml file will be used, not Resources\Layout\Main.axml. This thus requires that your layout files be semantically equivalent: if you have a button with a given ID in Resources\Layout\Main.axml, then Resources\Layout-land\Main.axml must also have a button with the same ID. Failure to keep your layouts consistent with each other will result in FindViewById() returning null for some files.

The fix is to ensure that your layouts are consistent: if you have buttons & fields in one layout, then the all all configurations should have the same buttons & fields (or you need to handle the "missing" IDs in your code).

 - Jon

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Loading...