Skip to content

Commit

Permalink
Update AmplitudeFlutterPlugin.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
Mercy811 committed Feb 29, 2024
1 parent c00d1c6 commit 5a042be
Showing 1 changed file with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ import android.content.pm.PackageManager
import com.amplitude.android.utilities.DefaultEventUtils
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import java.lang.ref.WeakReference

class AmplitudeFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
lateinit var amplitude: Amplitude
private var activity: Activity? = null
private var activity: WeakReference<Activity?> = WeakReference(null)
lateinit var ctxt: Context

private lateinit var channel: MethodChannel
Expand All @@ -33,19 +34,19 @@ class AmplitudeFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
}

override fun onAttachedToActivity(binding: ActivityPluginBinding) {
activity = binding.activity
activity = WeakReference(binding.activity)
}

override fun onDetachedFromActivityForConfigChanges() {
activity = null
activity = WeakReference(null)
}

override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
activity = binding.activity
activity = WeakReference(binding.activity)
}

override fun onDetachedFromActivity() {
activity = null
activity = WeakReference(null)
}

override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
Expand Down Expand Up @@ -163,7 +164,7 @@ class AmplitudeFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {

if (appLifecycles) {
val packageManager = ctxt.packageManager
var packageInfo = try {
val packageInfo = try {
packageManager.getPackageInfo(ctxt.packageName, 0)
} catch (ex: PackageManager.NameNotFoundException) {
println("Error occurred in getting package info. " + ex.message)
Expand All @@ -173,7 +174,7 @@ class AmplitudeFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
}

if (deepLinks) {
activity?.let { utils.trackDeepLinkOpenedEvent(it) }
activity.get()?.let { utils.trackDeepLinkOpenedEvent(it) }
}
}
}
Expand Down

0 comments on commit 5a042be

Please sign in to comment.