Skip to main content

Suspend and Resume

The SDK has suspend and resume methods that you need to call when you put your app into the background or return it into the foreground.

Because of protection mechanisms, the MC SDK does not allow the app to be brought into the background without its knowledge. Consequently, the app must ensure to set the SDK into suspended mode by calling suspend. It allows the app to run in background mode without the SDK being terminated. When the app returns to the foreground it has to call resume. Resume should only be invoked when the MC SDK has been suspended before.

⚠️ We recommend calling suspend and resume inside of the appropriate overrides/app life-cycle events of the operating system. It is imperative that you call the SDKs resume method on app start before calling anything else!

iOS/Swift

For iOS you would call suspend() inside of your applicationDidEnterBackground(), and resume() inside of your applicationWillEnterForeground(). Here is a code snippet:

Handling Suspend and resume (Swift)
extension AppDelegate {
...
func applicationDidEnterBackground(_ application: UIApplication) {
MasterControllerAdapter.sharedInstance.masterController?.suspend()
}

func applicationWillEnterForeground(_ application: UIApplication) {
MasterControllerAdapter.sharedInstance.masterController?.resume()
}
...
}

Android/Kotlin

For Android you would call suspendMasterController() inside of your onStop(), and resumeMasterController() inside of your onStart() application lifecycle override. Both methods can be found in the SynchronousEventHandler class of the SDK.

Generally we recommend using ProcessLifecycleOwner for the ease of use with multiple activities while having minimal or no downside for apps that use only one activity. However, when using the Lifecycle.Event.ON_STOP in some cases it might happen that the system does not leave enough time for the suspendMasterController() to complete (for example when the user kills the app in the same moment it has been suspended/stopped). In such case another option would be using the activity lifecycle while making sure suspendMasterController() is only being called when the app is not in foreground by checking ActivityManager.RunningAppProcessInfo. We do not recommend to depend on other checks here as we did not test other options than the stated one. For most users there should be no difference between using activity life cycle or ProcessLifecycleOwner.

If you are triggering a RestartEvent in your onStop, there is no need to call suspend since there might not be enough time left for both to complete anyway.

As the code you actually need highly depends on the Android API you actually use for managing suspend and resume, we refrain from giving a code sample.