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
- Add the Lifecycle extension and its dependency, the Mobile Core extension to your project using the app's Gradle file.
Kotlin
Copied to your clipboardimplementation(platform("com.adobe.marketing.mobile:sdk-bom:3.+"))implementation("com.adobe.marketing.mobile:core")implementation("com.adobe.marketing.mobile:lifecycle")
Groovy
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.
- Import the Lifecycle and MobileCore extensions in your application's main activity.
Copied to your clipboardimport com.adobe.marketing.mobile.MobileCore;import com.adobe.marketing.mobile.Lifecycle;
- Add the AEPLifecycle extension and its dependency, the Mobile Core extension, to your project using Cocoapods.
Add the following pods in your Podfile
:
Copied to your clipboardpod 'AEPCore', '~> 5.0'pod 'AEPLifecycle', '~> 5.0'
- Import the Lifecycle library:
Swift
Copied to your clipboardimport AEPCoreimport AEPLifecycle
Objective-C
Copied to your clipboard@import AEPCore;@import AEPLifecycle;
- Add the Lifecycle extension and its dependency, the Mobile Core extension to your project using the app's Gradle file.
Kotlin
Copied to your clipboardimplementation(platform("com.adobe.marketing.mobile:sdk-bom:3.+"))implementation("com.adobe.marketing.mobile:core")implementation("com.adobe.marketing.mobile:lifecycle")
Groovy
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.
- Import the Lifecycle and MobileCore extensions in your application's main activity.
Copied to your clipboardimport com.adobe.marketing.mobile.MobileCore;import com.adobe.marketing.mobile.Lifecycle;
- Add the AEPLifecycle extension and its dependency, the Mobile Core extension, to your project using Cocoapods.
Add the following pods in your Podfile
:
Copied to your clipboardpod 'AEPCore', '~> 5.0'pod 'AEPLifecycle', '~> 5.0'
- Import the Lifecycle library:
Swift
Copied to your clipboardimport AEPCoreimport AEPLifecycle
Objective-C
Copied to your clipboard@import AEPCore;@import AEPLifecycle;
Register Lifecycle with Mobile Core and add appropriate Start/Pause calls
Java
- Register the Lifecycle extension:
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 other post registration processing});}}
- In the
onResume
function, start the lifecycle data collection:
Copied to your clipboard@Overridepublic void onResume() {MobileCore.setApplication(getApplication());MobileCore.lifecycleStart(null);}
Setting the 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. We recommend that you call setApplication
in each of your activities.
- In the
onPause
function, pause the lifecycle data collection:
Copied to your clipboard@Overridepublic void onPause() {MobileCore.lifecyclePause();}
To ensure accurate session and crash reporting, this call must be added to every activity.
Swift
- Register the Lifecycle extension with the SDK Core by adding the following to your app's
application:didFinishLaunchingWithOptions:
delegate method. This will register the extension with Core and begin Lifecycle event processing:
Copied to your clipboardfunc application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {MobileCore.registerExtensions([Lifecycle.self, ...], {...}}
- Start Lifecycle data collection by calling
lifecycleStart:
from within the callback of theMobileCore.registerExtensions
method in your app'sapplication: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)}}}
- When launched, if your app is resuming from a backgrounded state, iOS might call your
applicationWillEnterForeground:
delegate method. You also need to calllifecycleStart:
, but this time you do not need all of the supporting code that you used inapplication:didFinishLaunchingWithOptions:
:
Copied to your clipboardfunc applicationWillEnterForeground(_ application: UIApplication) {MobileCore.lifecycleStart(additionalContextData: nil)}
In iOS 13 and later, for a scene-based application, use the UISceneDelegate
's sceneWillEnterForeground
method as follows:
Copied to your clipboardfunc sceneWillEnterForeground(_ scene: UIScene) {MobileCore.lifecycleStart(additionalContextData: nil)}
For more information on handling foregrounding applications with Scenes, refer to Apple's documentation here
- When the app enters the background, pause Lifecycle data collection from your app's
applicationDidEnterBackground:
delegate method:
Copied to your clipboardfunc applicationDidEnterBackground(_ application: UIApplication) {MobileCore.lifecyclePause()}
In iOS 13 and later, for a scene-based application, use the UISceneDelegate
's sceneDidEnterBackground
method as follows:
Copied to your clipboardfunc sceneDidEnterBackground(_ scene: UIScene) {MobileCore.lifecyclePause()}
For more information on handling backgrounding applications with Scenes, refer to Apple's documentation here
Objective-C
- Register the Lifecycle extension with the SDK Core by adding the following to your app's
application:didFinishLaunchingWithOptions:
delegate method. This will register the extension with Core and begin Lifecycle event processing:
Copied to your clipboard- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {// register the lifecycle extension[AEPMobileCore registerExtensions:@[AEPMobileLifecycle.class, ...] completion:^{...}];return YES;}
- Start Lifecycle data collection by calling
lifecycleStart:
from within the callback of theAEPMobileCore::registerExtensions:
method in your app'sapplication: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 clipboard- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {// register the lifecycle extension, and begin event processing with Coreconst 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];}}];}
- When launched, if your app is resuming from a backgrounded state, iOS might call your
applicationWillEnterForeground:
delegate method. You also need to calllifecycleStart:
, but this time you do not need all of the supporting code that you used inapplication:didFinishLaunchingWithOptions:
:
Copied to your clipboard- (void) applicationWillEnterForeground:(UIApplication *)application {[AEPMobileCore lifecycleStart:nil];}
In iOS 13 and later, for a scene-based application, use the UISceneDelegate
's sceneWillEnterForeground
method as follows:
Copied to your clipboard- (void) sceneWillEnterForeground:(UIScene *)scene {[AEPMobileCore lifecycleStart:nil];}
For more information on handling foregrounding applications with Scenes, refer to Apple's documentation here
- When the app enters the background, pause Lifecycle data collection from your app's
applicationDidEnterBackground:
delegate method:
Copied to your clipboard- (void) applicationDidEnterBackground:(UIApplication *)application {[AEPMobileCore lifecyclePause];}
In iOS 13 and later, for a scene-based application, use the UISceneDelegate
's sceneDidEnterBackground
method as follows:
Copied to your clipboard- (void) sceneDidEnterBackground:(UIScene *)scene {[AEPMobileCore lifecyclePause];}
For more information on handling backgrounding applications with Scenes, refer to Apple's documentation here
Java
- Register the Lifecycle extension:
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 other post registration processing});}}
- In the
onResume
function, start the lifecycle data collection:
Copied to your clipboard@Overridepublic void onResume() {MobileCore.setApplication(getApplication());MobileCore.lifecycleStart(null);}
Setting the 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. We recommend that you call setApplication
in each of your activities.
- In the
onPause
function, pause the lifecycle data collection:
Copied to your clipboard@Overridepublic void onPause() {MobileCore.lifecyclePause();}
To ensure accurate session and crash reporting, this call must be added to every activity.
Swift
- Register the Lifecycle extension with the SDK Core by adding the following to your app's
application:didFinishLaunchingWithOptions:
delegate method. This will register the extension with Core and begin Lifecycle event processing:
Copied to your clipboardfunc application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {MobileCore.registerExtensions([Lifecycle.self, ...], {...}}
- Start Lifecycle data collection by calling
lifecycleStart:
from within the callback of theMobileCore.registerExtensions
method in your app'sapplication: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)}}}
- When launched, if your app is resuming from a backgrounded state, iOS might call your
applicationWillEnterForeground:
delegate method. You also need to calllifecycleStart:
, but this time you do not need all of the supporting code that you used inapplication:didFinishLaunchingWithOptions:
:
Copied to your clipboardfunc applicationWillEnterForeground(_ application: UIApplication) {MobileCore.lifecycleStart(additionalContextData: nil)}
In iOS 13 and later, for a scene-based application, use the UISceneDelegate
's sceneWillEnterForeground
method as follows:
Copied to your clipboardfunc sceneWillEnterForeground(_ scene: UIScene) {MobileCore.lifecycleStart(additionalContextData: nil)}
For more information on handling foregrounding applications with Scenes, refer to Apple's documentation here
- When the app enters the background, pause Lifecycle data collection from your app's
applicationDidEnterBackground:
delegate method:
Copied to your clipboardfunc applicationDidEnterBackground(_ application: UIApplication) {MobileCore.lifecyclePause()}
In iOS 13 and later, for a scene-based application, use the UISceneDelegate
's sceneDidEnterBackground
method as follows:
Copied to your clipboardfunc sceneDidEnterBackground(_ scene: UIScene) {MobileCore.lifecyclePause()}
For more information on handling backgrounding applications with Scenes, refer to Apple's documentation here
Objective-C
- Register the Lifecycle extension with the SDK Core by adding the following to your app's
application:didFinishLaunchingWithOptions:
delegate method. This will register the extension with Core and begin Lifecycle event processing:
Copied to your clipboard- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {// register the lifecycle extension[AEPMobileCore registerExtensions:@[AEPMobileLifecycle.class, ...] completion:^{...}];return YES;}
- Start Lifecycle data collection by calling
lifecycleStart:
from within the callback of theAEPMobileCore::registerExtensions:
method in your app'sapplication: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 clipboard- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {// register the lifecycle extension, and begin event processing with Coreconst 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];}}];}
- When launched, if your app is resuming from a backgrounded state, iOS might call your
applicationWillEnterForeground:
delegate method. You also need to calllifecycleStart:
, but this time you do not need all of the supporting code that you used inapplication:didFinishLaunchingWithOptions:
:
Copied to your clipboard- (void) applicationWillEnterForeground:(UIApplication *)application {[AEPMobileCore lifecycleStart:nil];}
In iOS 13 and later, for a scene-based application, use the UISceneDelegate
's sceneWillEnterForeground
method as follows:
Copied to your clipboard- (void) sceneWillEnterForeground:(UIScene *)scene {[AEPMobileCore lifecycleStart:nil];}
For more information on handling foregrounding applications with Scenes, refer to Apple's documentation here
- When the app enters the background, pause Lifecycle data collection from your app's
applicationDidEnterBackground:
delegate method:
Copied to your clipboard- (void) applicationDidEnterBackground:(UIApplication *)application {[AEPMobileCore lifecyclePause];}
In iOS 13 and later, for a scene-based application, use the UISceneDelegate
's sceneDidEnterBackground
method as follows:
Copied to your clipboard- (void) sceneDidEnterBackground:(UIScene *)scene {[AEPMobileCore lifecyclePause];}
For more information on handling backgrounding applications with Scenes, refer to Apple's documentation here