Home » Xamarin.Forms Life Cycle

Xamarin.Forms Life Cycle


Xamarin.Forms Life Cycle

Xamarin.Form is a platform to develop the cross-platform mobile application by using the XAML for Front-End and C# for the backend of the application. In Xamarin.Form application, we can share all code. Xamarin.Forms also give the 100% API coverage of Android and iOS native APIs. So that, we can develop native Android, iOS, and Windows apps.

The life cycle of Xamarin.Forms applications:

When we create Xamarin.Forms application, we will see four projects:

  • Portable Project: This is the project where we will code 95% of our application code, and this code is shared in all the three platforms.
  • Android: This is the project where we set the Android application icon and splash screen, and all the other code will come from the portable project.
  • iOS: In this project, we set the iOS application architecture and icon.
  • Universal Windows: Universal Windows Platform is the application platform to build them both, Windows mobile and Windows desktop applications.

We can see all four projects in this screenshot:

Xamarin.Forms Life Cycle

Xamarin.Forms application lifecycle consists of three virtual methods that are overridden to handle the lifecycle method. These methods are present in App. Xaml. Cs class in a portable project.

We find all these files here, shown in the screenshot:

Xamarin.Forms Life Cycle

The three methods are:

  • OnStart()
  • OnSleep()
  • OnResume()

Xamarin.Forms Life Cycle

These three methods are called when the application is in the state of start, sleep, or resume. There is no method for the termination of the application. Application terminates from OnSleep() method without any additional notification.

We can see all these files in the App.Xaml.cs in our Xamarin.Forms (portable project).

OnStart() Method

OnStart() method calls when the application starts for the first time. When the application starts, it reads all the code written in the OnStart() method.

Syntax of OnStart() method can be written as below:

OnSleep() Method

OnSleep() method calls when the application is in the sleep mode, i.e., when there is no work in the application. Sleep method calls when the user hides the application. In this form, our application is open in the background in the sleep state.

Syntax of OnSleep() method can be written as below:

OnResume() Method

OnResume() method is called when we come back to the application from sleep mode.

Syntax of OnResume() method can be written as below:

Let’s put breakpoints on all these methods, and we will see all of these methods called when the application is in one of these states.

You may also like