Usage
Use DetourDelegate in Activity lifecycle
Section titled “Use DetourDelegate in Activity lifecycle”DetourDelegate is the recommended integration for classic Android apps. It wraps coroutine calls to Detour.processLink(...) and returns LinkResult in a callback.
import android.content.Intentimport android.os.Bundleimport androidx.appcompat.app.AppCompatActivityimport com.swmansion.detour.Detourimport com.swmansion.detour.DetourConfigimport com.swmansion.detour.DetourDelegateimport com.swmansion.detour.models.LinkProcessingModeimport com.swmansion.detour.models.LinkResult
class MainActivity : AppCompatActivity() { private val detourDelegate = DetourDelegate( lifecycleOwner = this, config = DetourConfig( apiKey = BuildConfig.DETOUR_API_KEY, appId = BuildConfig.DETOUR_APP_ID, linkProcessingMode = LinkProcessingMode.ALL, ), onLinkResult = { result -> handleLinkResult(result) } )
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) Detour.initialize(this, detourDelegate.config) detourDelegate.onCreate(intent) }
override fun onNewIntent(intent: Intent) { super.onNewIntent(intent) setIntent(intent) detourDelegate.onNewIntent(intent) }
private fun handleLinkResult(result: LinkResult) { // handle result and navigate }}If you want complete, runnable integrations for each navigation style, go to Examples.
LinkResult handling
Section titled “LinkResult handling”processLink() / getDeferredLink() return one of:
LinkResult.Success— containsurl,route,pathname,params,typeLinkResult.NotFirstLaunch— deferred flow already consumed in this install/sessionLinkResult.NoLink— no matching linkLinkResult.Error— runtime/integration error
Typical routing pattern:
when (result) { is LinkResult.Success -> navigateToRoute(result.route, result.params) is LinkResult.NotFirstLaunch -> Unit is LinkResult.NoLink -> Unit is LinkResult.Error -> Log.e("Detour", "Link error", result.exception)}Choose linkProcessingMode
Section titled “Choose linkProcessingMode”DetourConfig.linkProcessingMode controls which link sources are handled:
| Value | Deferred | App Links (http/https) | Custom scheme |
|---|---|---|---|
ALL (default) | Yes | Yes | Yes |
WEB_ONLY | Yes | Yes | No |
DEFERRED_ONLY | Yes | No | No |
Use DEFERRED_ONLY when your navigation stack already handles app-link/scheme intents and Detour should only resolve first-install deferred links.
Deferred-only manual flow
Section titled “Deferred-only manual flow”In DEFERRED_ONLY, you can skip DetourDelegate and call Detour.getDeferredLink() directly:
Detour.initialize( this, DetourConfig( apiKey = BuildConfig.DETOUR_API_KEY, appId = BuildConfig.DETOUR_APP_ID, linkProcessingMode = LinkProcessingMode.DEFERRED_ONLY, ))
lifecycleScope.launch { when (val result = Detour.getDeferredLink()) { is LinkResult.Success -> navigateToRoute(result.route, result.params) else -> Unit }}See the example-deferred-only app in Examples.
Analytics module
Section titled “Analytics module”DetourAnalytics is initialized together with Detour.initialize(...).
import com.swmansion.detour.analytics.DetourAnalyticsimport com.swmansion.detour.analytics.DetourEventNames
DetourAnalytics.logEvent( DetourEventNames.Purchase, mapOf("item" to "subscription_premium", "currency" to "USD"),)
DetourAnalytics.logRetention("home_screen_opened")