-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathActivityTracker.kt
More file actions
48 lines (40 loc) · 1.83 KB
/
Copy pathActivityTracker.kt
File metadata and controls
48 lines (40 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.mparticlesample
import android.app.Activity
import android.app.Application
import android.os.Bundle
import android.util.Log
import java.lang.ref.WeakReference
/**
* Mirrors the Rokt Android SDK's `ActivityLifeCycleObserver` caching strategy:
* the "current activity" is captured ONLY in onActivityResumed. This is the
* mechanism RoktModalActivity (overlay / bottom-sheet placements) depends on.
*
* We register two trackers in the repro:
* - EAGER : registered in Application.onCreate (before MainActivity exists)
* - DEFERRED: registered when MParticle.start() is called at first-frame paint
*
* If the deferred tracker is registered AFTER the host Activity has already
* resumed, it never sees onActivityResumed and currentActivity stays null --
* exactly the failure ROKT/sdk-android-source#1062 / #1063 fix.
*/
class ActivityTracker(private val label: String) : Application.ActivityLifecycleCallbacks {
@Volatile
private var currentActivityRef: WeakReference<Activity>? = null
val currentActivity: Activity?
get() = currentActivityRef?.get()
companion object {
const val TAG = "DeferredInitRepro"
}
override fun onActivityResumed(activity: Activity) {
currentActivityRef = WeakReference(activity)
Log.i(TAG, "[$label] onActivityResumed -> captured ${activity.localClassName}")
}
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
Log.i(TAG, "[$label] onActivityCreated ${activity.localClassName}")
}
override fun onActivityStarted(activity: Activity) {}
override fun onActivityPaused(activity: Activity) {}
override fun onActivityStopped(activity: Activity) {}
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}
override fun onActivityDestroyed(activity: Activity) {}
}