GoogleChrome / android-browser-helper

The Android Browser Helper library helps developers use Custom Tabs and Trusted Web Activities on top of the AndroidX browser support library.
Apache License 2.0
688 stars 286 forks source link

Intercepting url changed event #373

Open mohammad-zr opened 2 years ago

mohammad-zr commented 2 years ago

Is there anyway to catch url changed event in a twa? I want to do something when user navigated from our domain to a different domain...

sherryysj commented 8 months ago

I am also figuring out how to add an event when user navigate to another domain in the browser.

mohammad-zr commented 8 months ago

Inside AndroidManifest.xml put your activity like below:

<activity
            android:name=".PurchaseActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data
                    android:host="in-app-payment"
                    android:scheme="test" />
            </intent-filter>
        </activity>

and in your web application use the below code:

window.location.href = ``test://in-app-payment:${productId};``

Inside your activity code (koltin, java) you could get params like productId (in my example) as shown below:

private fun purchaseProduct() {
        val intent = intent
        val uri: Uri? = intent.data
        val productId = uri?.port.toString()
        ...
}