Lifecycle extension in Android
Implementing Lifecycle Metrics in Android
For implementation details, please reference the guide on registering Lifecycle with Mobile Core and adding the appropriate start/pause calls.
Tracking app crashes in Android
This information helps you understand how crashes are tracked and the best practices to handle false crashes.
App crashes are tracked as part of lifecycle metrics. Before you can track crashes, add the library to your project.
When lifecycle metrics are implemented, a call is made to MobileCore.lifecycleStart(additionalContextData)
in the OnResume
method of each activity. In the onPause
method, a call is made to MobileCore.lifecyclePause()
. In the MobileCore.lifecyclePause()
method, a flag is set to indicate a graceful exit. When the app is launched again or resumed, MobileCore.lifecycleStart(additionalContextData)
checks this flag. If the app did not exit successfully as determined by the flag status, an a.CrashEvent
context data is sent with the next call, and a crash event is reported.
To ensure accurate crash reporting, you must call lifecyclePause()
in the onPause
method of each activity.
To understand why this is essential, here is an illustration of the Android activity lifecycle:
For more information about the Android activity lifecycle, see Activities.
This Android lifecycle illustration was created and shared by the Android Open Source Project and used according to terms in the Creative Commons 2.5 Attribution License.
What can cause a false crash to be reported?
- If you are debugging by using an IDE such as Android Studio, and launching the app again from the IDE while the app is in the foreground causes a crash.
- You can avoid this crash by backgrounding the app before launching again from the IDE.
- If the previous foreground Activity of your app is moved to the background and does not call
MobileCore.lifecyclePause()
inonPause
, and your app is manually closed or killed by the operating system, the next launch results in a crash.
How should Fragments be handled?
Fragments have application lifecycle events that are similar to Activities. However, a Fragment cannot be active without being attached to an Activity.
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 AdobeMobileMarketing.lifecycleStart()
, and do not need to implement the code for each of the Activity.
Copied to your clipboardimport com.adobe.marketing.mobile.MobileCore;import com.adobe.marketing.mobile.Lifecycle;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);getApplication().registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {@Overridepublic void onActivityResumed(Activity activity) {MobileCore.setApplication(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) {}});}...}
To include additional data with lifecycle metric calls, pass an additional parameter to lifecycleStart
that contains context data:
Copied to your clipboard@Overridepublic void onResume() {HashMap<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.