- Notifications
You must be signed in to change notification settings - Fork19
Open a url in a browser or Chrome Custom Tab and receive a response as the result of user interaction.
License
braintree/browser-switch-android
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Android Browser Switch makes it easy to open a url in a browser orChrome Custom Tab and receive aresponse as the result of user interaction, either cancel or response data from the web page.
Add the library to your dependencies in yourbuild.gradle:
dependencies { implementation'com.braintreepayments.api:browser-switch:3.3.0'}To preview the latest work in progress builds, add the following SNAPSHOT dependency in yourbuild.gradle:
dependencies { implementation'com.braintreepayments.api:browser-switch:3.3.1-SNAPSHOT'}You will also need to add the Sonatype snapshots repo to your top-levelbuild.gradle to import SNAPSHOT builds:
allprojects { repositories { maven { url'https://oss.sonatype.org/content/repositories/snapshots/' } }}Declare an activity that you own as a deep link target in yourAndroidManifest.xml:
<activityandroid:name="com.myapp.MyDeepLinkTargetActivity"android:exported="true"> <intent-filter> <actionandroid:name="android.intent.action.VIEW"/> <dataandroid:scheme="my-custom-url-scheme"/> <categoryandroid:name="android.intent.category.DEFAULT"/> <categoryandroid:name="android.intent.category.BROWSABLE"/> </intent-filter></activity>
Note: The scheme you define must use all lowercase letters. This is due toscheme matching on the Android framework being case sensitive, expecting lower case. The scheme must also bevalid according to RFC 2396.
If these requirements are not met, an error will be returned and no browser switch will occur.
A browser switch can be initiated by callingBrowserSwitchClient#start(). UseBrowserSwitchOptions to configure options for browser switching:
val browserSwitchOptions=BrowserSwitchOptions().apply { requestCode=MY_REQUEST_CODE url="https://site-to-load.com?callbackURL=my-custom-url-scheme%3A%2F%2Fsuccess" returnUrlScheme="my-custom-url-scheme"}when (val pendingRequest= browserSwitchClient.start(this, browserSwitchOptions)) {isBrowserSwitchPendingRequest.Started-> {// store pending request }isBrowserSwitchPendingRequest.Failure-> {// browser was unable to be launched, handle failure }}
In the above example, notice the encodedcallbackURL parameter is forwarded to the website that will be loaded. The callback url must have the same custom scheme set inBrowserSwitchOptions. When this URL is loaded by the site, the Android OS will re-direct the user to the deep link destinationActivity defined in theAndroidManifest.xml.
To capture a browser switch result, override your deep link targetActivity with the following code snippet:
overridefunonResume() { handleReturnToAppFromBrowser(intent) }funhandleReturnToAppFromBrowser(intent:Intent) {// fetch stored pending request fetchPendingRequestFromPersistentStorage()?.let { startedRequest->when (val browserSwitchResult= browserSwitchClient.parseResult(startedRequest, intent)) {isBrowserSwitchResult.Success-> {// handle successful browser switch result// clear stored pending request }isBrowserSwitchResult.NoResult-> {// user did not complete browser switch// allow user to complete browser switch, or clear stored pending request } } }}
If your deep link targetActivity hasandroid:launchMode="singleTop",android:launchMode="singleTask", orandroid:launchMode="singleInstance", add the following code snippet to your deep link targetActivity in theonNewIntent method instead ofonResume:
overridefunonNewIntent(newIntent:Intent?) {super.onNewIntent(intent) handleReturnToAppFromBrowser(intent) }
This SDK abides by our Client SDK Deprecation Policy. For more information on the potential statuses of an SDK check ourdeveloper docs.
| Major version number | Status | Released | Deprecated | Unsupported |
|---|---|---|---|---|
| 3.x.x | Active | March 2025 | TBA | TBA |
| 2.x.x | Inactive | February 2021 | March 2026 | March 2027 |
| 1.x.x | Unsupported | June 2020 | April 2022 | April 2023 |
- Check out the reference docs
- Find a bug?Open an issue
- Want to contribute?Check out contributing guidelines andsubmit a pull request.
Android Browser Switch is open source and available under the MIT license. See theLICENSE file for more info.
About
Open a url in a browser or Chrome Custom Tab and receive a response as the result of user interaction.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors15
Uh oh!
There was an error while loading.Please reload this page.