Why is OnCreate Called Multiple Times?

Nathan Sokalski 4,106 Reputation points
2022-03-30T00:46:54.393+00:00

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). Based on the diagram at

https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/activity-lifecycle/#activity-lifecycle-methods

OnCreate is only called once. Am I misunderstanding something?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,362 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 76,391 Reputation points Microsoft Vendor
    2022-03-30T01:32:17.563+00:00

    Hello,​

    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.

    http://developer.android.com/guide/topics/resources/runtime-changes.html

    Please add 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  
    

    Best Regards,

    Leon Lu


    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.