Home > database >  Xamarin Android Screen Orientation
Xamarin Android Screen Orientation

Time:02-01

I'm programming a WebView application for android (blank Xamarin project / Visual Studio 2022). I added this line so the WebView will not reload on orientation change:

[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize)]

This works on the Pixel 5 - API 30 (Android 11.0) Emulator (Debug), but when I install it as APK (Release) on my Galaxy Note 20 the application just shows blank white screen.

Without the:

ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize)

The application works just fine, but keep reloading on orientation change.

I'm expecting the application will not reload on orientation change and return the user to the homepage which is called via: webView.LoadUrl("url"); and remain on the current web page.

CodePudding user response:

Issue resolved by changing on Release:

  • "Linking" to "None" on Android Options
  • unchecking "Define TRACE constant" on Build
  • unchecking "Optimize Code" on Build

Removing

ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize

added this to OnCreate():

if (savedInstanceState == null) 
{
     webView.LoadUrl("url");
}

and these:

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

protected override void OnRestoreInstanceState(Bundle savedInstanceState)
{
    base.OnRestoreInstanceState(savedInstanceState);
    webView.RestoreState(savedInstanceState);
}
  • Related