Skip to content

Commit

Permalink
added lifecycle events to java
Browse files Browse the repository at this point in the history
  • Loading branch information
mrehan27 committed Sep 28, 2023
1 parent d60b333 commit 486c105
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package io.customer.android.sample.java_layout;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.HashMap;
import java.util.Map;

import io.customer.sdk.CustomerIO;
import io.customer.sdk.CustomerIOShared;
import io.customer.sdk.util.Logger;

public class ActivityLifecycleEventsListener implements Application.ActivityLifecycleCallbacks {

private final String lifecycleEventKey = "ActivityLifecycleEvent";
private Logger logger = CustomerIOShared.instance().getDiStaticGraph().getLogger();

public void logEvent(@NonNull String eventName, @NonNull Activity activity) {
logger.debug(String.format("%s: %s %s", lifecycleEventKey, eventName, activity.getClass().getSimpleName()));

try {
CustomerIO sdkInstance = CustomerIO.instance();
Map<String, String> extras = new HashMap<>();
extras.put("EventName", eventName);
extras.put("ActivityName", activity.getClass().getSimpleName());
sdkInstance.track(lifecycleEventKey, extras);
} catch (Exception e) {
logger.debug("SDK not yet initialized");
}
}

@Override
public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle bundle) {
logEvent("onActivityCreated", activity);
}

@Override
public void onActivityStarted(@NonNull Activity activity) {
logEvent("onActivityStarted", activity);
}

@Override
public void onActivityResumed(@NonNull Activity activity) {
logEvent("onActivityResumed", activity);
}

@Override
public void onActivityPaused(@NonNull Activity activity) {
logEvent("onActivityPaused", activity);
}

@Override
public void onActivityStopped(@NonNull Activity activity) {
logEvent("onActivityStopped", activity);
}

@Override
public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle bundle) {
logEvent("onActivitySaveInstanceState", activity);
}

@Override
public void onActivityDestroyed(@NonNull Activity activity) {
logEvent("onActivityDestroyed", activity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@

public class SampleApplication extends Application {
private final ApplicationGraph appGraph = new ApplicationGraph(this);
public ActivityLifecycleEventsListener lifecycleEventsListener;

@Override
public void onCreate() {
super.onCreate();
// Initialize Customer.io SDK on app start
appGraph.getCustomerIORepository().initializeSdk(SampleApplication.this);
lifecycleEventsListener = new ActivityLifecycleEventsListener();
registerActivityLifecycleCallbacks(lifecycleEventsListener);
}

public ApplicationGraph getApplicationGraph() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.customer.android.sample.java_layout.ui.core;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.EmptySuper;
Expand All @@ -19,6 +20,12 @@ public abstract class BaseActivity<VB extends ViewBinding> extends AppCompatActi

protected abstract VB inflateViewBinding();

@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
((SampleApplication) getApplication()).lifecycleEventsListener.logEvent("onNewIntent", this);
}

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down

0 comments on commit 486c105

Please sign in to comment.