Bolt UIX
Bolt UIX

Reputation: 7052

How to Use Custom Chrome Tabs in Jetpack Compose?

How to implementation of Custom Chrome Tabs in Jetpack compose?

Is there any lib for Chrome tab? Please share reference link or sample code.

Upvotes: 4

Views: 3568

Answers (4)

idrisadetunmbi
idrisadetunmbi

Reputation: 1027

You can also use

rememberLauncherForActivityResult(contract = StartActivityForResult())

Create your custom tab intent with this, and launch the custom tab as you would like with any other instantiated launcher:

CustomTabsIntent.Builder().build().intent.apply { 
    setData(url) 
}

Upvotes: 0

svg
svg

Reputation: 309

fun Context.launchCustomTabs(url: String, useIncognito: Boolean?) {
    CustomTabsIntent.Builder().build().apply {
        if (useIncognito == true) { 
            intent.putExtra(
                "com.google.android.apps.chrome.EXTRA_OPEN_NEW_INCOGNITO_TAB", 
                true
            )
        }
    }
    .launchUrl(this, Uri.parse(url))
}

From your compose code:

val context = LocalContext.current
....
....
onClick = {
   context.launchCustomTabs(url = "https://google.com", useIncognito = false)
}
....

Upvotes: 3

Bolt UIX
Bolt UIX

Reputation: 7052

implementation 'androidx.browser:browser:1.4.0'

val context = LocalContext.current
var mCustomTabsServiceConnection: CustomTabsServiceConnection? = null
var mClient: CustomTabsClient? = null
var mCustomTabsSession: CustomTabsSession? = null

mCustomTabsServiceConnection = object : CustomTabsServiceConnection() {
     override fun onCustomTabsServiceConnected(componentName: ComponentName, customTabsClient: CustomTabsClient) {
         // Pre-warming
         mClient = customTabsClient
         mClient?.warmup(0L)
         mCustomTabsSession = mClient?.newSession(null)
    }

    override fun onServiceDisconnected(name: ComponentName) {
        mClient = null
    }
}

CustomTabsClient.bindCustomTabsService(context, "com.android.chrome", mCustomTabsServiceConnection)
val customTabsIntent = CustomTabsIntent.Builder(mCustomTabsSession)
    // .setToolbarColor(color)
    .setShowTitle(true)
    .build()
    
customTabsIntent.launchUrl(context, Uri.parse("https://google.com"))

Upvotes: 3

Jakoss
Jakoss

Reputation: 5275

Custom chrome tabs have nothing to do with compose since there is no UI render on your part. Use them like you would in View system, just make sure you are using context from LocalContext.current

Upvotes: 4

Related Questions