2222
2323namespace Tizen . Applications
2424{
25+ /// <summary>
26+ /// Enumeration for application lifecycle state.
27+ /// </summary>
28+ [ EditorBrowsable ( EditorBrowsableState . Never ) ]
29+ public enum ApplicationLifecycleState
30+ {
31+ /// <summary>The application is initialized.</summary>
32+ Initialized = 0 ,
33+ /// <summary>The application is created.</summary>
34+ Created = 1 ,
35+ /// <summary>The application is resumed.</summary>
36+ Resumed = 2 ,
37+ /// <summary>The application is paused.</summary>
38+ Paused = 3 ,
39+ /// <summary>The application is destroyed.</summary>
40+ Destroyed = 4
41+ }
42+
43+ /// <summary>
44+ /// Event arguments for application lifecycle state changed event.
45+ /// </summary>
46+ [ EditorBrowsable ( EditorBrowsableState . Never ) ]
47+ public class ApplicationLifecycleStateChangedEventArgs : EventArgs
48+ {
49+ /// <summary>
50+ /// Gets the application ID.
51+ /// </summary>
52+ public string ApplicationId { get ; internal set ; }
53+
54+ /// <summary>
55+ /// Gets the process ID.
56+ /// </summary>
57+ public int ProcessId { get ; internal set ; }
58+
59+ /// <summary>
60+ /// Gets the lifecycle state.
61+ /// </summary>
62+ public ApplicationLifecycleState State { get ; internal set ; }
63+
64+ /// <summary>
65+ /// Gets whether the application has focus.
66+ /// </summary>
67+ public bool HasFocus { get ; internal set ; }
68+ }
69+
2570 /// <summary>
2671 /// This class has the methods and events of the ApplicationManager.
2772 /// </summary>
@@ -38,6 +83,9 @@ public static class ApplicationManager
3883 private static IntPtr _eventHandle = IntPtr . Zero ;
3984 private static readonly object s_eventLock = new object ( ) ;
4085 private static readonly object s_applicationChangedEventLock = new object ( ) ;
86+ private static EventHandler < ApplicationLifecycleStateChangedEventArgs > s_lifecycleStateChangedHandler ;
87+ private static Interop . ApplicationManager . AppManagerLifecycleStateChangedCallback s_lifecycleStateChangedCallback ;
88+ private static readonly object s_lifecycleStateChangedLock = new object ( ) ;
4189
4290 /// <summary>
4391 /// Occurs whenever the installed application is enabled.
@@ -687,6 +735,85 @@ private static void UnRegisterApplicationEvent()
687735 }
688736 }
689737
738+ /// <summary>
739+ /// Occurs when the lifecycle state of any application changes.
740+ /// </summary>
741+ /// <remarks>
742+ /// This event is raised whenever the lifecycle state of any application changes.
743+ /// It provides information about the application whose lifecycle state has changed.
744+ /// </remarks>
745+ /// <exception cref="ArgumentException">Thrown when failed of invalid argument.</exception>
746+ /// <exception cref="InvalidOperationException">Thrown when failed to register the lifecycle state changed event.</exception>
747+ /// <example>
748+ /// The following code snippet demonstrates how to subscribe to the ApplicationLifecycleStateChanged event:
749+ ///
750+ /// <code>
751+ /// ApplicationManager.ApplicationLifecycleStateChanged += (sender, e) =>
752+ /// {
753+ /// Console.WriteLine($"Application {e.ApplicationId} (PID: {e.ProcessId}) state changed to {e.State}");
754+ /// Console.WriteLine($"Has focus: {e.HasFocus}");
755+ /// };
756+ /// </code>
757+ /// </example>
758+ /// <since_tizen> 13 </since_tizen>
759+ [ EditorBrowsable ( EditorBrowsableState . Never ) ]
760+ public static event EventHandler < ApplicationLifecycleStateChangedEventArgs > ApplicationLifecycleStateChanged
761+ {
762+ add
763+ {
764+ lock ( s_lifecycleStateChangedLock )
765+ {
766+ if ( s_lifecycleStateChangedCallback == null )
767+ {
768+ RegisterLifecycleStateChangedEvent ( ) ;
769+ }
770+ s_lifecycleStateChangedHandler += value ;
771+ }
772+ }
773+ remove
774+ {
775+ lock ( s_lifecycleStateChangedLock )
776+ {
777+ s_lifecycleStateChangedHandler -= value ;
778+ if ( s_lifecycleStateChangedHandler == null && s_lifecycleStateChangedCallback != null )
779+ {
780+ UnRegisterLifecycleStateChangedEvent ( ) ;
781+ s_lifecycleStateChangedCallback = null ;
782+ }
783+ }
784+ }
785+ }
786+
787+ private static void RegisterLifecycleStateChangedEvent ( )
788+ {
789+ s_lifecycleStateChangedCallback = ( string appId , int pid ,
790+ Interop . ApplicationManager . AppLifecycleState state , bool hasFocus , IntPtr userData ) =>
791+ {
792+ lock ( s_lifecycleStateChangedLock )
793+ {
794+ s_lifecycleStateChangedHandler ? . Invoke ( null , new ApplicationLifecycleStateChangedEventArgs
795+ {
796+ ApplicationId = appId ,
797+ ProcessId = pid ,
798+ State = ( ApplicationLifecycleState ) state ,
799+ HasFocus = hasFocus
800+ } ) ;
801+ }
802+ } ;
803+
804+ Interop . ApplicationManager . ErrorCode err =
805+ Interop . ApplicationManager . AppManagerSetLifecycleStateChangedCb ( s_lifecycleStateChangedCallback , IntPtr . Zero ) ;
806+ if ( err != Interop . ApplicationManager . ErrorCode . None )
807+ {
808+ throw ApplicationManagerErrorFactory . GetException ( err , "Failed to register the lifecycle state changed event." ) ;
809+ }
810+ }
811+
812+ private static void UnRegisterLifecycleStateChangedEvent ( )
813+ {
814+ Interop . ApplicationManager . AppManagerUnsetLifecycleStateChangedCb ( ) ;
815+ }
816+
690817 /// <summary>
691818 /// Gets the information of the recent applications.
692819 /// </summary>
0 commit comments