Configuring Life Cycle Events in .NET MAUI Apps | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (173).NET Core  (29).NET MAUI  (203)Angular  (107)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (40)Black Friday Deal  (1)Blazor  (211)BoldSign  (13)DocIO  (24)Essential JS 2  (106)Essential Studio  (200)File Formats  (65)Flutter  (132)JavaScript  (219)Microsoft  (118)PDF  (81)Python  (1)React  (98)Streamlit  (1)Succinctly series  (131)Syncfusion  (897)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (50)Windows Forms  (61)WinUI  (68)WPF  (157)Xamarin  (161)XlsIO  (35)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (146)Chart  (127)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (618)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (39)Extensions  (22)File Manager  (6)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (501)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (42)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (10)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (381)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (17)Web  (582)What's new  (323)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Configuring Life Cycle Events in .NET MAUI Apps

Configuring Life Cycle Events in .NET MAUI Apps

Managing app life cycle events is one of the most common requirements when developing an application. Likewise, it’s necessary to handle the app life cycle in a cross-platform application like a .NET Multi-platform App UI (MAUI) app to make it more efficient.

In this blog, I will share how to configure app life cycle events in your .NET MAUI app with code examples.

App life cycle

Generally, an app has different life cycles or states. A .NET MAUI app has the following four life cycles (execution states):

  • Running
  • Not running
  • Deactivated
  • Stopped

Different events will be triggered when the window moves to each state.

Cross-platform life cycles

Following are some of the predefined life cycle events available in the cross-platform apps:

  • Created: Occurs when an app moves from the not running state to the running state. Generally, when we launch a new window.
  • Activated: Occurs when a window moves from the unfocused state to the focused state (unfocused= going behind another window).
  • Deactivated: Occurs when a window moves to the unfocused state.
  • Stopped: Occurs when the window becomes hidden. For example, when we minimize it. With this, there is a chance that the window might be destroyed.
  • Resumed: A follow-up occurrence of the stopped event but not like a created event.
  • Destroying: Occurs when the window gets destroyed and deallocated.

Syncfusion’s .NET MAUI controls suite is the expert’s choice for building modern web apps.

Following is the mapping diagram that explains how the .NET MAUI framework will map the native events.

.NET MAUI App Life Cycle Events Platform Mapping
.NET MAUI App Life Cycle Events Platform Mapping

How to configure life cycle events

With .NET MAUI Preview 13, you can easily configure a life cycle event in the MauiProgram class using the MauiAppBuilder and the ConfigureLifecycleEvents extension method. This method is available in Microsoft.Maui.LifecycleEvents namespace.

Refer to the following code example for the common configuration.

using Microsoft.Maui.LifecycleEvents;
public static class MauiProgram
{
   public static MauiApp CreateMauiApp()
   {
     var builder = MauiApp.CreateBuilder();
     builder.ConfigureLifecycleEvents(AppLifecycle => {
             //
            // code to raise an event.
           //
     });
     return builder.Build();
   }
}

Platform-specific life cycle events

You can also raise platform-specific events for a custom setup.

Android

Currently, the following 21 Android platform-specific events are available:

  • OnActivityResult
  • OnApplicationConfigurationChanged
  • OnApplicationCreate
  • OnApplicationCreating
  • OnApplicationLowMemory
  • OnApplicationTrimMemory
  • OnBackPressed
  • OnConfigurationChanged
  • OnCreate
  • OnDestroy
  • OnNewIntent
  • OnPause
  • OnPostCreate
  • OnPostResume
  • OnRequestPermissionsResult
  • OnRestart
  • OnRestoreInstanceState
  • OnResume
  • OnSaveInstanceState
  • OnStart
  • OnStop

You can use the compiler directives to invoke the Android platform-specific events using the AddAndroid() extension method.

Every property of the Syncfusion .NET MAUI controls is completely documented to make it easy to get started.

Refer to the following code example. Here, we are going to invoke the OnBackPressed event to move to the previous destination.

Public static MauiApp CreateMauiApp()
{
  var builder = MauiApp.CreateBuilder();
  builder.UseMauiApp<App>()
  builder.ConfigureLifecycleEvents(AppLifecycle => {
       #if ANDROID
         AppLifecycle.AddAndroid(android => android
            .OnBackPressed((activity) => BackPressed()));
       #endif
  });
  return builder.Build();
}
static bool BackPressed()
{
  return true;
}

iOS

Currently, the following 10 iOS platform-specific events are available:

  • ContinueUserActivity
  • DidEnterBackground
  • FinishedLaunching
  • OnActivated
  • OnResignActivation
  • OpenUrl
  • PerformActionForShortcutItem
  • WillEnterForeground
  • WillFinishLaunching
  • WillTerminate

You can use the compiler directives to invoke the iOS platform-specific events using the AddiOS() extension method.

Refer to the following code example. Here, we are going to invoke the WillEnterForeground event that will raise when the app is in focus mode.

Public static MauiApp CreateMauiApp()
{
  var builder = MauiApp.CreateBuilder();
  builder.UseMauiApp<App>()
  builder.ConfigureLifecycleEvents(AppLifecycle => {
      #if IOS
       AppLifecycle.AddiOS(ios => ios
          .WillEnterForeground((app) => EnteredForeground())
       );
      #endif
  });
  return builder.Build();
}
static void EnterForeground()
{
}

Windows

Currently, the following 8 Windows platform-specific events are available:

  • OnActivated
  • OnClosed
  • OnLaunched
  • OnLaunching
  • OnNativeMessage
  • OnResumed
  • OnVisibilityChanged
  • OnWindowCreated

You can use the compiler directives to invoke the Windows platform-specific events using the AddWindows() extension method.

Refer to the following code example. Here, we will invoke the OnNativeMessage event to access the app instance and remove the title bar.

Syncfusion .NET MAUI controls allow you to build powerful line-of-business applications.

Public static MauiApp CreateMauiApp()
{
  var builder = MauiApp.CreateBuilder();
  builder.UseMauiApp<App>()
  builder.ConfigureLifecycleEvents(AppLifecycle => {
      #if WINDOWS
        AppLifecycle
         .AddWindows(windows =>
           windows.OnNativeMessage((app, args) => {
             app.ExtendsContentIntoTitleBar = false;
           }));
      #endif
  });
  return builder.Build();
}
Removed the Title Bar in .NET MAUI Windows App
Removed the Title Bar in .NET MAUI Windows App

Reference

For more details, refer to the .NET MAUI App Life Cycle Events documentation.

Conclusion

Thanks for reading! In this blog, we have seen the .NET MAUI app life cycle events and how to configure them for better efficiency. Try out them and let us know your feedback in the comments below.

We at Syncfusion are working on our .NET MAUI controls. Check out our .NET MAUI controls road map for plans for our upcoming 2022 Volume 1 release and find more on our controls in their documentation.

If you have questions, you can contact us through our support forumsupport tickets, or feedback portal. We are always happy to assist you!

Test Flight
App Center Badge
Google Play Store Badge
Microsoft Badge
Github Store Badge

Related blogs

Tags:

Share this post:

Comments (1)

Peter Van Vogelpoel
Peter Van Vogelpoel

Hi, very helpfull. But can you explain how to transfer an event (let’s say OnResume) to a razor page in my Maui Blazor app?

Peter

Comments are closed.

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed