Lifecycle
Sessions contain information about the app's current lifecycle, such as the device information, the application install or upgrade information, the session start and pause times, the number of application launches, and additional context data that is provided by the developer through the lifecycleStart
API. Session data is persisted, so it is available across application launches.
Add Lifecycle to your app
(Android)
Add the Lifecycle extension and its dependency, the Mobile Core extension to your project using the app's Gradle file.
Copied to your clipboardimplementation(platform("com.adobe.marketing.mobile:sdk-bom:3.+"))implementation("com.adobe.marketing.mobile:core")implementation("com.adobe.marketing.mobile:lifecycle")
Using dynamic dependency versions is not recommended for production apps. Please read the managing Gradle dependencies guide for more information.
(Android)
Add the Lifecycle extension and its dependency, the Mobile Core extension to your project using the app's Gradle file.
Copied to your clipboardimplementation platform('com.adobe.marketing.mobile:sdk-bom:3.+')implementation 'com.adobe.marketing.mobile:core'implementation 'com.adobe.marketing.mobile:lifecycle'
Using dynamic dependency versions is not recommended for production apps. Please read the managing Gradle dependencies guide for more information.
(iOS)
Add the AEPLifecycle extension and its dependency, AEPCore extension, to your project using CocoaPods.
Add the following pods in your Podfile
:
Copied to your clipboardpod 'AEPCore', '~> 5.0'pod 'AEPLifecycle', '~> 5.0'
Add the Lifecycle extension and its dependency, the Mobile Core extension to your project using the app's Gradle file.
Copied to your clipboardimplementation(platform("com.adobe.marketing.mobile:sdk-bom:3.+"))implementation("com.adobe.marketing.mobile:core")implementation("com.adobe.marketing.mobile:lifecycle")
Using dynamic dependency versions is not recommended for production apps. Please read the managing Gradle dependencies guide for more information.
Add the Lifecycle extension and its dependency, the Mobile Core extension to your project using the app's Gradle file.
Copied to your clipboardimplementation platform('com.adobe.marketing.mobile:sdk-bom:3.+')implementation 'com.adobe.marketing.mobile:core'implementation 'com.adobe.marketing.mobile:lifecycle'
Using dynamic dependency versions is not recommended for production apps. Please read the managing Gradle dependencies guide for more information.
Add the AEPLifecycle extension and its dependency, AEPCore extension, to your project using CocoaPods.
Add the following pods in your Podfile
:
Copied to your clipboardpod 'AEPCore', '~> 5.0'pod 'AEPLifecycle', '~> 5.0'
Register Lifecycle with Mobile Core
(Android)
Kotlin
Register the Lifecycle extension in your app's Application
class:
Copied to your clipboardimport com.adobe.marketing.mobile.MobileCoreimport com.adobe.marketing.mobile.Lifecycle...
Copied to your clipboardclass MobileApp : Application() {override fun onCreate() {super.onCreate()MobileCore.setApplication(this);val extensions = listOf(Lifecycle.EXTENSION, ...)MobileCore.registerExtensions(extensions) {// Any post registration processing}}}
(Android)
Java
Register the Lifecycle extension in your app's Application
class:
Copied to your clipboardimport com.adobe.marketing.mobile.MobileCore;import com.adobe.marketing.mobile.Lifecycle;...
Copied to your clipboardpublic class MobileApp extends Application {@Overridepublic void onCreate() {super.onCreate();MobileCore.setApplication(this);List<Class<? extends Extension>> extensions = Arrays.asList(Lifecycle.EXTENSION, ...);MobileCore.registerExtensions(extensions, o -> {// Any post registration processing});}}
(iOS)
Register the Lifecycle extension in the app delegate object's application(_:didFinishLaunchingWithOptions:)
method:
Copied to your clipboardimport AEPCoreimport AEPLifecycle...
Copied to your clipboardfunc application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {MobileCore.registerExtensions([Lifecycle.self, ...], {// Any post registration processing}}
(iOS)
Register the Lifecycle extension in the app delegate object's application:didFinishLaunchingWithOptions:
method:
Copied to your clipboard@import AEPCore;@import AEPLifecycle;...
Copied to your clipboard- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {[AEPMobileCore registerExtensions:@[AEPMobileLifecycle.class, ...] completion:^{// Any post registration processing}];return YES;}
Kotlin
Register the Lifecycle extension in your app's Application
class:
Copied to your clipboardimport com.adobe.marketing.mobile.MobileCoreimport com.adobe.marketing.mobile.Lifecycle...
Copied to your clipboardclass MobileApp : Application() {override fun onCreate() {super.onCreate()MobileCore.setApplication(this);val extensions = listOf(Lifecycle.EXTENSION, ...)MobileCore.registerExtensions(extensions) {// Any post registration processing}}}
Java
Register the Lifecycle extension in your app's Application
class:
Copied to your clipboardimport com.adobe.marketing.mobile.MobileCore;import com.adobe.marketing.mobile.Lifecycle;...
Copied to your clipboardpublic class MobileApp extends Application {@Overridepublic void onCreate() {super.onCreate();MobileCore.setApplication(this);List<Class<? extends Extension>> extensions = Arrays.asList(Lifecycle.EXTENSION, ...);MobileCore.registerExtensions(extensions, o -> {// Any post registration processing});}}
Register the Lifecycle extension in the app delegate object's application(_:didFinishLaunchingWithOptions:)
method:
Copied to your clipboardimport AEPCoreimport AEPLifecycle...
Copied to your clipboardfunc application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {MobileCore.registerExtensions([Lifecycle.self, ...], {// Any post registration processing}}
Register the Lifecycle extension in the app delegate object's application:didFinishLaunchingWithOptions:
method:
Copied to your clipboard@import AEPCore;@import AEPLifecycle;...
Copied to your clipboard- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {[AEPMobileCore registerExtensions:@[AEPMobileLifecycle.class, ...] completion:^{// Any post registration processing}];return YES;}
Add Lifecycle start and pause calls
You can start collecting Lifecycle information at any time in your app, but you should start as soon as your app enters the foreground. This allows Lifecycle metrics to be correctly attributed to all of your users' activities for their current session.
You should pause Lifecycle collection when the user stops using your app. The best time to do this is usually when your app has entered the background.
Lifecycle on iOS
Start Lifecycle data collection on launch
Start Lifecycle data collection by calling lifecycleStart(_:)
from within the callback of the MobileCore.registerExtensions(_:)
method in your app's application(_:didFinishLaunchingWithOptions:)
delegate method.
If your iOS application supports background capabilities, your application(_:didFinishLaunchingWithOptions:)
method might be called when iOS launches your app in the background. If you do not want background launches to count towards your lifecycle metrics, then lifecycleStart(_:)
should only be called when the application state is not equal to UIApplicationStateBackground
.
Copied to your clipboardfunc application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {let appState = application.applicationStateMobileCore.registerExtensions([Lifecycle.self, ...], {if appState != .background {// only start lifecycle if the application is not in the backgroundMobileCore.lifecycleStart(additionalContextData: nil)}}}
Copied to your clipboard- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {const UIApplicationState appState = application.applicationState;[AEPMobileCore registerExtensions:@[AEPMobileLifecycle.class, ...] completion:^{// only start lifecycle if the application is not in the backgroundif (appState != UIApplicationStateBackground) {[AEPMobileCore lifecycleStart:nil];}}];}
Copied to your clipboardfunc application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {let appState = application.applicationStateMobileCore.registerExtensions([Lifecycle.self, ...], {if appState != .background {// only start lifecycle if the application is not in the backgroundMobileCore.lifecycleStart(additionalContextData: nil)}}}
Copied to your clipboard- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {const UIApplicationState appState = application.applicationState;[AEPMobileCore registerExtensions:@[AEPMobileLifecycle.class, ...] completion:^{// only start lifecycle if the application is not in the backgroundif (appState != UIApplicationStateBackground) {[AEPMobileCore lifecycleStart:nil];}}];}
Start and Pause Lifecycle data collection from iOS lifecycle delegate
When your app is resuming from the background state, call lifecycleStart(_:)
from the appropriate delegate object's "will enter foreground" method. When your app enters the background state, call lifecyclePause()
from the appropriate delegate object's "did enter background" method.
For a scene-based UI, call the Lifecycle APIs from the UISceneDelegate's
sceneWillEnterForeground(_:)
andsceneDidEnterBackground(_:)
methods.For all other apps, call the Lifecycle APIs from the UIApplicationDelegate's
applicationWillEnterForeground(_:)
andapplicationDidEnterBackground(_:)
methods.If your application supports both a scene delegate and an app delegate, implement the Lifecycle APIs in both delegate objects.
In iOS 13 and later, for a scene-based application, use the UISceneDelegate
as follows:
Copied to your clipboardfunc sceneWillEnterForeground(_ scene: UIScene) {MobileCore.lifecycleStart(additionalContextData: nil)}
Copied to your clipboardfunc sceneDidEnterBackground(_ scene: UIScene) {MobileCore.lifecyclePause()}
In iOS 12 and earlier, use the UIApplicationDelegate
as follows:
Copied to your clipboardfunc applicationWillEnterForeground(_ application: UIApplication) {MobileCore.lifecycleStart(additionalContextData: nil)}
Copied to your clipboardfunc applicationDidEnterBackground(_ application: UIApplication) {MobileCore.lifecyclePause()}
In iOS 13 and later, for a scene-based application, use the UISceneDelegate
as follows:
Copied to your clipboard- (void) sceneWillEnterForeground:(UIScene *)scene {[AEPMobileCore lifecycleStart:nil];}
Copied to your clipboard- (void) sceneDidEnterBackground:(UIScene *)scene {[AEPMobileCore lifecyclePause];}
In iOS 12 and earlier, use the UIApplicationDelegate
as follows:
Copied to your clipboard- (void) applicationWillEnterForeground:(UIApplication *)application {[AEPMobileCore lifecycleStart:nil];}
Copied to your clipboard- (void) applicationDidEnterBackground:(UIApplication *)application {[AEPMobileCore lifecyclePause];}
In iOS 13 and later, for a scene-based application, use the UISceneDelegate
as follows:
Copied to your clipboardfunc sceneWillEnterForeground(_ scene: UIScene) {MobileCore.lifecycleStart(additionalContextData: nil)}
Copied to your clipboardfunc sceneDidEnterBackground(_ scene: UIScene) {MobileCore.lifecyclePause()}
In iOS 12 and earlier, use the UIApplicationDelegate
as follows:
Copied to your clipboardfunc applicationWillEnterForeground(_ application: UIApplication) {MobileCore.lifecycleStart(additionalContextData: nil)}
Copied to your clipboardfunc applicationDidEnterBackground(_ application: UIApplication) {MobileCore.lifecyclePause()}
In iOS 13 and later, for a scene-based application, use the UISceneDelegate
as follows:
Copied to your clipboard- (void) sceneWillEnterForeground:(UIScene *)scene {[AEPMobileCore lifecycleStart:nil];}
Copied to your clipboard- (void) sceneDidEnterBackground:(UIScene *)scene {[AEPMobileCore lifecyclePause];}
In iOS 12 and earlier, use the UIApplicationDelegate
as follows:
Copied to your clipboard- (void) applicationWillEnterForeground:(UIApplication *)application {[AEPMobileCore lifecycleStart:nil];}
Copied to your clipboard- (void) applicationDidEnterBackground:(UIApplication *)application {[AEPMobileCore lifecyclePause];}
For more information on handling foreground and background states in applications with Scenes, refer to Apple's documentation on preparing your UI to run in the foreground and background
Start and Pause Lifecycle data collection in SwiftUI
If your pure SwiftUI application does not use an app delegate or scene delegate, you may still use the Lifecycle extension by listening for scenePhase
changes.
Register the Lifecycle extension and configure the Mobile SDK from the
App
class'sinit()
function.Set the
@Environment
property wrapper to observe thescenePhase
variable to read the application's current phase.Use the
scenePhase
property in conjunction with.onChange(of:)
to trigger the Lifecycle APIs when the phase changes between.active
and.background
.
Copied to your clipboardimport SwiftUIimport AEPCoreimport AEPLifecycle@mainstruct TestSwiftUIApp: App {@Environment(\.scenePhase) private var scenePhaseinit() {MobileCore.registerExtensions([Lifecycle.self]) {// Post registration tasks, such as configureWith(appId:)}}var body: some Scene {WindowGroup {ContentView()}.onChange(of: scenePhase) { phase inswitch phase {case .active:MobileCore.lifecycleStart(additionalContextData: nil)case .background:MobileCore.lifecyclePause()case .inactive:print("Inactive scene phase")@unknown default:print("unknown scene phase has been added to scenePhase enum")}}}}
For more information, read the full blog post Implement Adobe Experience Cloud Mobile Lifecycle Tracking in SwiftUI.
Include additional context data
To include additional data with lifecycle tracking calls, pass an additional parameter to lifecycleStart(additionalContextData:)
that contains context data:
Copied to your clipboardMobileCore.lifecycleStart(additionalContextData: ["myapp.category": "Game"])
Copied to your clipboard[AEPMobileCore lifecycleStart:@{@"myapp.category": @"Game"}];
Copied to your clipboardMobileCore.lifecycleStart(additionalContextData: ["myapp.category": "Game"])
Copied to your clipboard[AEPMobileCore lifecycleStart:@{@"myapp.category": @"Game"}];
Lifecycle on Android
Start and Pause Lifecycle data collection from Android Activity
To ensure accurate session and crash reporting, the Lifecycle APIs must be implemented in every Activity of the Android Application. Do not start or stop Lifecycle in a Fragment.
Kotlin
Add the following to each Android Activity class.
Copied to your clipboardimport com.adobe.marketing.mobile.MobileCoreimport com.adobe.marketing.mobile.Lifecycle...
Copied to your clipboardoverride fun onResume() {MobileCore.setApplication(this.application)MobileCore.lifecycleStart(null)}
Copied to your clipboardoverride fun onPause() {MobileCore.lifecyclePause()}
Java
Add the following to each Android Activity class.
Copied to your clipboardimport com.adobe.marketing.mobile.MobileCore;import com.adobe.marketing.mobile.Lifecycle;...
Copied to your clipboard@Overridepublic void onResume() {MobileCore.setApplication(getApplication());MobileCore.lifecycleStart(null);}
Copied to your clipboard@Overridepublic void onPause() {MobileCore.lifecyclePause();}
Kotlin
Add the following to each Android Activity class.
Copied to your clipboardimport com.adobe.marketing.mobile.MobileCoreimport com.adobe.marketing.mobile.Lifecycle...
Copied to your clipboardoverride fun onResume() {MobileCore.setApplication(this.application)MobileCore.lifecycleStart(null)}
Copied to your clipboardoverride fun onPause() {MobileCore.lifecyclePause()}
Java
Add the following to each Android Activity class.
Copied to your clipboardimport com.adobe.marketing.mobile.MobileCore;import com.adobe.marketing.mobile.Lifecycle;...
Copied to your clipboard@Overridepublic void onResume() {MobileCore.setApplication(getApplication());MobileCore.lifecycleStart(null);}
Copied to your clipboard@Overridepublic void onPause() {MobileCore.lifecyclePause();}
Calling setApplication(Application)
is only necessary on activities that are entry points for your application. However, setting the application on each Activity has no negative impact and ensures that the SDK always has the necessary reference to your application. You should call setApplication(Application)
in each of your activities.
Implementing global lifecycle callbacks
Starting with API Level 14, Android allows global lifecycle callbacks for activities. For more information, please read the Android developer guide.
You can use these callbacks to ensure that all of your activities correctly call the Lifecycle APIs without needing to update each individual Activity class. Add code to register an instance of ActivityLifecycleCallbacks in your Application class, just before registering your extensions with MobileCore.
Copied to your clipboardimport com.adobe.marketing.mobile.MobileCoreimport com.adobe.marketing.mobile.Lifecycleclass MobileApp : Application() {override fun onCreate() {super.onCreate()registerActivityLifecycleCallbacks(object: ActivityLifecycleCallbacks {override fun onActivityResumed(activity: Activity) {MobileCore.setApplication(activity.application)MobileCore.lifecycleStart(null)}override fun onActivityPaused(activity: Activity) {MobileCore.lifecyclePause()}// the following methods aren't needed for our lifecycle purposes, but are// required to be implemented by the ActivityLifecycleCallbacks objectoverride fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {}override fun onActivityStarted(activity: Activity) {}override fun onActivityStopped(activity: Activity) {}override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}override fun onActivityDestroyed(activity: Activity) {}})...}...}
Copied to your clipboardimport com.adobe.marketing.mobile.MobileCore;import com.adobe.marketing.mobile.Lifecycle;public class MobileApp extends Application {@Overrideprotected void onCreate() {super.onCreate();registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {@Overridepublic void onActivityResumed(Activity activity) {MobileCore.setApplication(activity.getApplication());MobileCore.lifecycleStart(null);}@Overridepublic void onActivityPaused(Activity activity) {MobileCore.lifecyclePause();}// the following methods aren't needed for our lifecycle purposes, but are// required to be implemented by the ActivityLifecycleCallbacks object@Overridepublic void onActivityCreated(Activity activity, Bundle savedInstanceState) {}@Overridepublic void onActivityStarted(Activity activity) {}@Overridepublic void onActivityStopped(Activity activity) {}@Overridepublic void onActivitySaveInstanceState(Activity activity, Bundle outState) {}@Overridepublic void onActivityDestroyed(Activity activity) {}});...}...}
Copied to your clipboardimport com.adobe.marketing.mobile.MobileCoreimport com.adobe.marketing.mobile.Lifecycleclass MobileApp : Application() {override fun onCreate() {super.onCreate()registerActivityLifecycleCallbacks(object: ActivityLifecycleCallbacks {override fun onActivityResumed(activity: Activity) {MobileCore.setApplication(activity.application)MobileCore.lifecycleStart(null)}override fun onActivityPaused(activity: Activity) {MobileCore.lifecyclePause()}// the following methods aren't needed for our lifecycle purposes, but are// required to be implemented by the ActivityLifecycleCallbacks objectoverride fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {}override fun onActivityStarted(activity: Activity) {}override fun onActivityStopped(activity: Activity) {}override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}override fun onActivityDestroyed(activity: Activity) {}})...}...}
Copied to your clipboardimport com.adobe.marketing.mobile.MobileCore;import com.adobe.marketing.mobile.Lifecycle;public class MobileApp extends Application {@Overrideprotected void onCreate() {super.onCreate();registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {@Overridepublic void onActivityResumed(Activity activity) {MobileCore.setApplication(activity.getApplication());MobileCore.lifecycleStart(null);}@Overridepublic void onActivityPaused(Activity activity) {MobileCore.lifecyclePause();}// the following methods aren't needed for our lifecycle purposes, but are// required to be implemented by the ActivityLifecycleCallbacks object@Overridepublic void onActivityCreated(Activity activity, Bundle savedInstanceState) {}@Overridepublic void onActivityStarted(Activity activity) {}@Overridepublic void onActivityStopped(Activity activity) {}@Overridepublic void onActivitySaveInstanceState(Activity activity, Bundle outState) {}@Overridepublic void onActivityDestroyed(Activity activity) {}});...}...}
Include additional context data
To include additional data with lifecycle tracking calls, pass an additional parameter to lifecycleStart(Map)
that contains context data:
Copied to your clipboardMobileCore.lifecycleStart(mapOf("myapp.category" to "Game"))
Copied to your clipboardHashMap<String, Object> additionalContextData = new HashMap<String, Object>();contextData.put("myapp.category", "Game");MobileCore.lifecycleStart(additionalContextData);
Copied to your clipboardMobileCore.lifecycleStart(mapOf("myapp.category" to "Game"))
Copied to your clipboardHashMap<String, Object> additionalContextData = new HashMap<String, Object>();contextData.put("myapp.category", "Game");MobileCore.lifecycleStart(additionalContextData);
You only need to add this code in your main Activity and any other Activity in which your app may be launched.