Problem when rotating android cell screen

Asked

Viewed 904 times

0

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, the webbrowser reloads the page ? what can I do to prevent this from happening and continue on the logged in page without scrolling? Thank you

  • You implement some function that is fired when turning the screen?

  • 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

3 answers

3


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

1

0

To complement this, a shortcut you can turn to is the use of setRetainInstance(true);, so your persistent problems when your Activity or Fragment is destroyed will be solved in part. xD

  • I wouldn’t call it a shortcut (otherwise it would be the official method, rs). It is important to understand the life cycle of Activities. The recommendation is that the developer always make the management of the state changes of the Activity to ensure that the application will not stick. This method you quoted only works for Fragment and should only be used when you want to deal with some large processing that should not be lost in the recreation of Activity (Ex: Asynctask, Threads, etc). But even so, there is no guarantee that Android will keep the state if the features are low and then the app can take.

  • Oops, thanks for the warning.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.