ReviewOS

also looking at this

craft-native/craft

fix(android): hand the launch link to the page that subscribes for it

#232
Merged glennmichael123 wants to merge fix/android-deep-link-replay into main
17 files +776 -136
packages/android/templates/MainActivity.kt.templatemodified+19-8
Changes to packages/android/templates/MainActivity.kt.template
@@ -44,7 +44,19 @@ class MainActivity : AppCompatActivity() {
4444
4545 webView = findViewById(R.id.webview)
4646 craftBridge = CraftBridge(this, webView, CraftHealthConnect(this, webView))
47 handleIncomingDeepLink(intent, dispatch = false)
47 // The link that launched the app (#215). Queued until the page's bridge
48 // is injected, then dispatched once with `initial: true`, so a page that
49 // subscribes rather than calling getInitialURL still hears it. A
50 // relaunch from Recents carries the old intent, and was not opened by it.
51 // A recreated activity restores what the bridge saved: its intent may
52 // be a later warm link that onNewIntent set.
53 if (savedInstanceState == null) {
54 val launchLink = intent.data?.toString()
55 val launchedFromHistory = intent.flags and Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY != 0
56 if (launchLink != null && !launchedFromHistory) craftBridge.receiveDeepLink(launchLink)
57 } else {
58 craftBridge.restoreDeepLinks(savedInstanceState)
59 }
4860 if (savedInstanceState == null) handleIncomingShortcut(intent)
4961
5062 setupWebView()
@@ -150,12 +162,6 @@ class MainActivity : AppCompatActivity() {
150162 }
151163 }
152164
153 private fun handleIncomingDeepLink(source: Intent?, dispatch: Boolean) {
154 val url = source?.data?.toString() ?: return
155 craftBridge.setInitialURL(url)
156 if (dispatch) craftBridge.dispatchDeepLink(url)
157 }
158
159165 private fun handleIncomingShortcut(source: Intent?) {
160166 source?.getStringExtra(SHORTCUT_TYPE_EXTRA)?.let(craftBridge::dispatchShortcut)
161167 }
@@ -198,10 +204,15 @@ class MainActivity : AppCompatActivity() {
198204 return (uri.scheme == "https" || localDevelopment) && origin in trustedOrigins
199205 }
200206
207 override fun onSaveInstanceState(outState: Bundle) {
208 super.onSaveInstanceState(outState)
209 craftBridge.saveDeepLinks(outState)
210 }
211
201212 override fun onNewIntent(intent: Intent) {
202213 super.onNewIntent(intent)
203214 setIntent(intent)
204 handleIncomingDeepLink(intent, dispatch = true)
215 intent.data?.toString()?.let(craftBridge::receiveDeepLink)
205216 handleIncomingShortcut(intent)
206217 }
207218