简体   繁体   中英

Xamarin Android possible ScreenOrientation.Landscape bug

my app had some strange behaviour so I recreated the app until the point where it bugged and found out that ScreenOrientation.Landscape is the culprit.

If you make a new blank app in visual studio 15 and replace the MainActivity with:

[Activity(Label = "TestLandscapeBug", MainLauncher = true, Icon = "@drawable/icon",
    ScreenOrientation = ScreenOrientation.Landscape)]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
    System.Console.WriteLine("OnCreate");
    base.OnCreate(bundle);

    SetContentView(Resource.Layout.Main);
}
protected override void OnDestroy()
{
    System.Console.WriteLine("OnDestroy");
    base.OnDestroy();
}
protected override void OnPause()
{
    System.Console.WriteLine("OnPause");
    base.OnPause();
}
protected override void OnRestart()
{
    System.Console.WriteLine("OnRestart");
    base.OnRestart();
}
protected override void OnResume()
{
    System.Console.WriteLine("OnResume");
    base.OnResume();
}
protected override void OnStart()
{
    System.Console.WriteLine("OnStart");
    base.OnStart();
}

protected override void OnStop()
{
    System.Console.WriteLine("OnStop");
    base.OnStop();
}

}

run the app and press the sleep button: OnPause, OnStop, OnDestroy, OnCreate, OnStart, OnResume and OnPause are called.

if you remove ScreenOrientation = ScreenOrientation.Landscape OnPause and OnStop are called.

Is this a bug? Or am I doing something wrong? how can I fix this or use something else which locks the screen in landscape.

It's normal for an activity to switch to portrait mode when the screen is locked. Whenever the orientation changes, OnDestroy is called followed by OnCreate. So, there's nothing to worry about as what you're witnessing is default behavior of Android.

Portrait is kind of the default orientation for the lockscreen so it makes sense that your activity also switches to that when locking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM