2017-02-17 42 views
0

我的應用程序有一些奇怪的行爲,所以我重新創建應用程序,直到它被竊聽,發現ScreenOrientation.Landscape是罪魁禍首。Xamarin Android可能ScreenOrientation.Landscape錯誤

如果您在Visual Studio 15新的空白應用程序並將其替換在MainActivity:

[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(); 
} 

} 

運行應用程序,並按下睡眠按鈕: 的onPause,調用OnStop,的OnDestroy,在OnCreate,OnStart中,的onResume和OnPause被調用。

如果您刪除ScreenOrientation = ScreenOrientation.Landscape OnPause和OnStop被調用。

這是一個錯誤?或者我做錯了什麼? 我該如何解決這個問題或者使用別的方法鎖定橫屏中的屏幕。

回答

1

當屏幕鎖定時,活動切換到肖像模式是正常現象。每當方向改變時,OnDestroy被OnCreate調用。所以,沒有什麼可擔心的,因爲你目睹的是Android的默認行爲。

肖像是一種默認取向的鎖屏,所以它是有道理的,你的活動也切換到鎖定時。

+1

您也可以使用[Actitity(ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.ScreenSize)]來阻止重新創建。 –

+0

@ b.holz很高興知道,沒有想到這一點! – hankide