Problem when rotating Android mobile screen

Hello, I am developing an application for android Using xamarin, I have in the application a webbrowser, very simple, where it accesses a login page ( http://portaldopatchwork.klickmembersproject.com.br/login ), then after I log in to this normal page, if I rotate the screen the page goes back to the login screen, why ? when the screen is turned, does webbrowser reload the page ? what can I do so that this does not occur and continues on the page of logged in without unplugging? Thank you

Author: ramaral, 2017-05-02

3 answers

Should add the flag configChanges with the value keyboardHidden, orientation and screenSize in your Activity

No Shamarim

You can add the flags with Android.Content.PM.ConfigChanges more or less like this:

[Activity (Label = "@string/app_name", MainLauncher = true, Icon="@drawable/launcher",
    ConfigurationChanges = ConfigChanges.KeyboardHidden | ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : ...

No Manifest.xml

I'm not sure if Xamarin provides control over Manifest.xml generated, but if available in the tag <activity> add android:configChanges="keyboardHidden|orientation|screenSize", 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 required (or if it depends on a newer version of Android), but if the previous configuration is not enough then save the instances:

If 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
}

Documentation Xamarin

 3
Author: Guilherme Nascimento, 2017-05-26 00:22:23

Activities on Android are destroyed and recreated when the screen rotates. You need to save the necessary data in a Bundle before it is destroyed in the onSaveInstanceState () method and then restore the Bundle data in OnCreate if it is not null.

More details here: https://developer.android.com/guide/topics/resources/runtime-changes.html

 1
Author: Márcio Oliveira, 2017-05-03 00:48:40

To supplement, a shortcut you can resort to is the use of setRetainInstance (true);, so your problems with persistence when your activity or fragment is destroyed will be solved, in part. xD

 0
Author: Jonathan Cedrim De Souza, 2017-05-02 21:10:46