Should add the flag configChanges
with the value keyboardHidden
, orientation
and screenSize
in his Activity
No Xamarim
You can add the flags
with Android.Content.PM.ConfigChanges
sort of like this:
[Activity (Label = "@string/app_name", MainLauncher = true, Icon="@drawable/launcher",
ConfigurationChanges = ConfigChanges.KeyboardHidden | ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : ...
In Manifest.xml
I’m not sure if Xamarin provides control over the generated Manifest.xml, but makes it available on the tag <activity>
add android:configChanges="keyboardHidden|orientation|screenSize"
, so for example:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"
android:configChanges="keyboardHidden|orientation|screenSize">
Saving instances
I don’t know if for Xamarin this is necessary (or if it depends on a newer version of Android), but if the previous configuration is not enough then save the instances:
If you’re in Csharp
Add to methods (or create them) with override:
private WebView minhaWebView; //Se a sua webView nesta variável
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
...
}
protected override void OnSaveInstanceState(Bundle outState)
{
base.OnSaveInstanceState(outState); //Salva Activity
minhaWebView.SaveState(outState); //Salva WebView
}
protected override void OnRestoreInstanceState(Bundle savedInstanceState)
{
base.OnRestoreSaveInstanceState(savedInstanceState); //Restaura o Activity
minhaWebView.RestoreState(savedInstanceState); //Restaura o WebView
}
If you are in Java
private WebView minhaWebView; //Defina a webView nesta variável
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
}
@Override
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState); //Salva Activity
minhaWebView.saveState(outState); //Salva WebView
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState); //Restaura o Activity
minhaWebView.restoreState(savedInstanceState); //Restaura o WebView
}
Documentação Xamarin
You implement some function that is fired when turning the screen?
– Oralista de Sistemas
no, I didn’t set anything up, I basically just created a webbrowser, I put it up on this site and that’s it, that’s it
– Gabriel Longatti