This article is for Android Mobile Applications running Android Services.
The complexity in applications utilizing Android Services is that when a user closes or kills the application, while a service is running, the application object stays alive due to its connection to the service.
This causes the Application:onCreate method not to be called if we reopen the app while a service is running, this causes our initialization code to not be called (as Penod SDK initialization relies the Application:onCreate method).
In order to ensure consistent behavior and data, you will need to use three APIs:
- endSession() - This API when called from the service’s onTaskRemoved() method, will ensure the session is cleaned when the app is being closed by the user.
- initSdkWithoutVisitor(...) - This API should be called from Application:onCreate. This API will ensure we do the basic initialization the SDK needs, without actually getting guides nor sending analytics.
- switchVisitor(...) - This API should be called from any context in the app, where you already have the visitor id available. (Usually called from an Activity’s onCreate method)
Note: using endSession can cause unexpected behavior such as guides not showing after the app returns to the foreground.
End Session API
This API should be called in the service’s onTaskRemoved method.
public static void endSession()
What will happen when you call endSession method?
- The visitor ID and account ID will be cleaned
- No guides will be shown
- No analytics will be sent
How do I resume the session?
A session can be resumed by calling the switchVisitor() method.