Lifecycle extension in Android

Implementing Lifecycle Metrics in Android

Tracking app crashes in Android

This information helps you understand how crashes are tracked and the best practices to handle false crashes.
Important: App crashes are tracked as part of lifecycle metrics. Before you can track crashes, add the library to your project. For more information about implementing lifecycle, see Implementing Lifecycle Metrics in Android.
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.
Important: 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.
    Tip: 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()in onPause, 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, see the Android Developers 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.
1
import com.adobe.marketing.mobile.*;
2
3
public class MainActivity extends Activity {
4
5
@Override
6
protected void onCreate(Bundle savedInstanceState) {
7
super.onCreate(savedInstanceState);
8
setContentView(R.layout.activity_main);
9
10
getApplication().registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
11
@Override
12
public void onActivityResumed(Activity activity) {
13
MobileCore.setApplication(getApplication());
14
MobileCore.lifecycleStart(null);
15
}
16
@Override
17
public void onActivityPaused(Activity activity) {
18
MobileCore.lifecyclePause();
19
}
20
// the following methods aren't needed for our lifecycle purposes, but are
21
// required to be implemented by the ActivityLifecycleCallbacks object
22
@Override
23
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {}
24
@Override
25
public void onActivityStarted(Activity activity) {}
26
@Override
27
public void onActivityStopped(Activity activity) {}
28
@Override
29
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {}
30
@Override
31
public void onActivityDestroyed(Activity activity) {}
32
});
33
}
34
...
35
}
Copied!
To include additional data with lifecycle metric calls, pass an additional parameter to lifecycleStart that contains context data:
1
@Overridepublic
2
void onResume() {
3
HashMap<String, Object> additionalContextData = new HashMap<String, Object>();
4
contextData.put("myapp.category", "Game");
5
MobileCore.lifecycleStart(additionalContextData);}
Copied!
Important: You need to add this code only in your main Activity and any other Activity in which your app may be launched.
Last modified 1yr ago