From 0fbaa8e65854633ce6059b428f2c292fccb0b127 Mon Sep 17 00:00:00 2001 From: Kenichi Tatsuhama Date: Tue, 3 Nov 2026 09:54:10 +0900 Subject: [PATCH] fix(android): deliver deep links opened while the app is in the foreground When a OneLink is opened while the app is already in the foreground (for example, from a notification), Android only pauses the Activity around onNewIntent and never stops it. The SDK resolves warm-start deep links when the Activity resumes after being stopped, so the registered DeepLinkListener was never called in this case. Plugin 6.18.0 fixed the same case by calling performOnDeepLinking from the onNewIntent listener; that call was lost in the SDK 7 rewrite. Forward the VIEW intent to AppsFlyerLib.performDeepLinking instead, but only when the Activity has not been stopped since it last resumed: when the app comes back from the background the SDK resolves the intent itself, and forwarding it as well would deliver the deep link twice. Co-Authored-By: Claude Opus 5.5 --- .../appsflyersdk/AppsflyerSdkPlugin.kt | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/android/src/main/kotlin/com/appsflyer/appsflyersdk/AppsflyerSdkPlugin.kt b/android/src/main/kotlin/com/appsflyer/appsflyersdk/AppsflyerSdkPlugin.kt index 9f8f564b..b3b04172 100644 --- a/android/src/main/kotlin/com/appsflyer/appsflyersdk/AppsflyerSdkPlugin.kt +++ b/android/src/main/kotlin/com/appsflyer/appsflyersdk/AppsflyerSdkPlugin.kt @@ -2,10 +2,15 @@ package com.appsflyer.appsflyersdk import android.app.Activity import android.content.Context +import android.content.Intent import android.os.Handler import android.os.Looper import android.util.Log +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.LifecycleEventObserver +import androidx.lifecycle.LifecycleOwner + import com.appsflyer.AppsFlyerLib import com.appsflyer.pluginbridge.handler.AppsFlyerRpcHandler import com.appsflyer.pluginbridge.model.RpcErrorCodes @@ -98,9 +103,25 @@ open class AppsflyerSdkPlugin : MethodCallHandler, FlutterPlugin, ActivityAware if (currentActivity != null) { currentActivity.intent = intent } + forwardDeepLinkIntentIfNotStopped(intent) false } + // Lifecycle of the attached Activity, observed so that onNewIntent can tell whether the + // Activity has been stopped since it last resumed. + private var observedLifecycle: Lifecycle? = null + + @Volatile + private var stoppedSinceLastResume = false + + private val activityLifecycleObserver = LifecycleEventObserver { _, event -> + when (event) { + Lifecycle.Event.ON_STOP -> stoppedSinceLastResume = true + Lifecycle.Event.ON_RESUME -> stoppedSinceLastResume = false + else -> Unit + } + } + private val eventStreamHandler: EventChannel.StreamHandler = object : EventChannel.StreamHandler { override fun onListen(arguments: Any?, events: EventChannel.EventSink?) { @@ -197,14 +218,55 @@ open class AppsflyerSdkPlugin : MethodCallHandler, FlutterPlugin, ActivityAware detachActivity() } + /** + * Resolves the deep link of a `VIEW` intent delivered while the app stays in the foreground. + * + * The SDK resolves warm-start deep links when the app comes back to the foreground, that is, + * when the Activity resumes after having been stopped. When the app is already in the + * foreground (for example, a OneLink in a notification tapped while the app is open), Android + * only pauses the Activity around `onNewIntent` and never stops it, so the SDK does not resolve + * the intent and the registered `DeepLinkListener` is not called. Plugin 6.18.0 fixed the same + * case by calling `performOnDeepLinking` here; SDK 7 replaces it with `performDeepLinking`. + * + * When the Activity has been stopped (the app comes back from the background), the SDK + * resolves the intent itself once the Activity resumes, so forwarding it here would deliver it + * twice. The session is left to the session-ready `start()` call. + */ + private fun forwardDeepLinkIntentIfNotStopped(intent: Intent) { + if (observedLifecycle == null || stoppedSinceLastResume) return + if (intent.action != Intent.ACTION_VIEW) return + val url = intent.dataString + if (url.isNullOrEmpty()) return + try { + AppsFlyerLib.getInstance().performDeepLinking(url, false) + } catch (t: Throwable) { + Log.e(AF_PLUGIN_TAG, "performDeepLinking failed for new intent: ${t.message}", t) + } + } + private fun attachActivity(binding: ActivityPluginBinding) { activity = binding.activity AppsFlyerContextHolder.attachActivity(this, binding.activity) binding.addOnNewIntentListener(onNewIntentListener) + observeActivityLifecycle(binding.activity) + } + + private fun observeActivityLifecycle(activity: Activity) { + stopObservingActivityLifecycle() + val lifecycle = (activity as? LifecycleOwner)?.lifecycle ?: return + lifecycle.addObserver(activityLifecycleObserver) + observedLifecycle = lifecycle + } + + private fun stopObservingActivityLifecycle() { + observedLifecycle?.removeObserver(activityLifecycleObserver) + observedLifecycle = null + stoppedSinceLastResume = false } private fun detachActivity() { AppsFlyerContextHolder.detachActivity(this) + stopObservingActivityLifecycle() activity = null }