Convenient helper class to launch web page through Google Chrome Custom Tab.
- Add
jcenter()
to your the rootbuild.gradle
allprojects scope.
allprojects {
repositories {
google()
jcenter() // <== add this one
}
}
- Add the dependency
minSdkVersion is 21
implementation "liou.rayyuan.chromecustomtabhelper:chrome-custom-tab-helper:[latest-version]"
- Create a new object for
ChromeCustomTabHelper
private val chromeCustomTabHelper = ChromeCustomTabsHelper()
- Warm up the custom tab service in the
onResume()
state of Activity or Fragment
And don't forget to cool down in theonStop()
state. You can pick up your prefer browser in the second parameter.
Current support browsers: Chrome, Firefox and Samsung internet.
override fun onResume() {
super.onResume()
chromeCustomTabHelper.bindCustomTabsServices(this, Browsers.CHROME, url)
}
override fun onStop() {
super.onStop()
chromeCustomTabHelper.unbindCustomTabsServices(this)
}
- When it need to open web tab.
val builder = CustomTabsIntent.Builder()
// here custom your tab styles and behavior
val chromeCustomTabIntent = builder.build()
ChromeCustomTabsHelper.openCustomTab(this, Browsers.CHROME, chromeCustomTabIntent, uri) { activity, uri ->
// if chrome is not install, fallback to standard webview
// or just send an intent to someone can handle
val intent = Intent(Intent.ACTION_VIEW)
intent.data = uri
startActivity(intent)
}
That's it!