I have a single-Activity app for which I am working on handling rotation. In all my other apps I did this by handling OnPause & OnResume. I thought OnCreate was called only once (when the >app first starts), but it seems like it is being called every time (each time I rotate my device it goes OnPause -> OnCreate -> OnResume)
Activity is recreated after each rotation by default. You can refer to this thread about more information.
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize| ConfigChanges.Keyboard|ConfigChanges.KeyboardHidden to the [Activity] tag above
MainActivity like following code. Add this ConfigurationChanges, OnCreate will be execute onetime. avoid the activity restart, then you can declare that your activity handles the configuration change itself which prevents the system from restarting your activity. (Note: I add Keyboard configchanges as well, If you do not added it, when keyboard hide and show, your activity will be restarted as well ).
[Activity(Label = "App22", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Keyboard|ConfigChanges.KeyboardHidden)] public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.