Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between OnStart and the App Constructor [duplicate]

Xamarin Forms has the following App class:

public class App : Application
{
        public App()
        {
            // The root page of your application
            MainPage = new ContentPage
            {
                Content = new StackLayout
                {
                    VerticalOptions = LayoutOptions.Center,
                    Children = {
                        new Label {
                            XAlign = TextAlignment.Center,
                            Text = "Welcome to Xamarin Forms!"
                        }
                    }
                }
            };
        }

        protected override void OnStart()
        {
            // Handle when your app starts
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }
}

QUESTION: What is the difference between the code that runs in the constructor and the code written in the OnStart method. Aren't both run when your application starts?

see http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/app-lifecycle/ for more information.

like image 335
Yoeri Avatar asked Mar 11 '15 13:03

Yoeri


1 Answers

They are completely different however the documentation is less than sparse.

Constructors are platform-agnostic and for the purpose of creating the object (apologies if this sounds like 'teaching you to suck eggs').

The OnStart() method however is mapped to the platform specific notification and its relevant meaning. This is the cross-platform implementation of each varying operating systems start up notification system - this will of course be different between the platforms but this abstraction allows you to handle it in an identical fashion.

like image 113
JordanMazurke Avatar answered Oct 13 '22 23:10

JordanMazurke