Skip to content
Open a url in a browser or Chrome Custom Tab and receive a response as the result of user interaction.
Java Shell
Branch: master
Clone or download
quinnjn Link to scheme matching Android docs (#14)
* Link to scheme matching Android docs

There have been queries on why browser-switch requires lowercase letters. It is because the Android framework considers schemes case insensitive.

* Android is case sensitive, not insensitive
Latest commit d830de2 Feb 14, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github Standardize ISSUE_TEMPLATE.md Feb 25, 2017
browser-switch
demo **Breaking Changes** Migrate to support fragments (#10) Oct 8, 2018
gradle/wrapper
.gitignore Initial commit Nov 18, 2016
.travis.yml
ACKNOWLEDGEMENTS.md
CHANGELOG.md Release 0.2.0 Oct 8, 2018
LICENSE Update LICENSE file year Feb 24, 2017
README.md Link to scheme matching Android docs (#14) Feb 14, 2019
build.gradle
gradlew Initial commit Nov 18, 2016
gradlew.bat Initial commit Nov 18, 2016
release.sh Don't ask for sonatype username or password if already present Mar 22, 2018
settings.gradle Add demo app Jul 3, 2017

README.md

Android Browser Switch

Build Status

Android Browser Switch makes it easy to open a url in a browser or Chrome Custom Tab and receive a response as the result of user interaction, either cancel or response data from the web page.

Setup

Add the library to your dependencies in your build.gradle:

dependencies {
  compile 'com.braintreepayments:browser-switch:0.2.0'
}

Declare BrowserSwitchActivity in your AndroidManifest.xml:

<activity android:name="com.braintreepayments.browserswitch.BrowserSwitchActivity"
    android:launchMode="singleTask">
    <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <data android:scheme="${applicationId}.browserswitch"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
    </intent-filter>
</activity>

Your app's url scheme must begin with your app's package and end with .browserswitch. For example, if the package is com.your-company.your-app, then your url scheme should be com.your-company.your-app.browserswitch.

${applicationId} is automatically applied with your app's package when using Gradle.

Note: The scheme you define must use all lowercase letters. This is due to scheme matching on the Android framework being case sensitive, expecting lower case. If your package contains underscores, the underscores should be removed when specifying the scheme.

If these requirements are not met, an error will be returned and no browser switch will occur.

Usage

BrowserSwitchFragment is an abstract androidx.fragment.app.Fragment that should be extended and used to start and handle the response from a browser switch.

Note: The Activity that BrowserSwitchFragment attaches to cannot have a launch mode of singleInstance. BrowserSwitchFragment needs access to the calling Activity to provide a result and cannot do so if the browser switch happens on a different activity stack.

The url scheme to use to return to your app can be retrieved using:

browserSwitchFragment.getReturnUrlScheme();

This scheme should be used to build a return url and passed along in the browser switch to the target web page. This url can be loaded to return to the app from the target web page.

A browser switch can be initiated by calling:

browserSwitchFragment.browserSwitch(requestCode, "http://example.com/");
// or
browserSwitchFragment.browserSwitch(requestCode, intent);

The response will be returned in your implementation of BrowserSwitchFragment#onBrowserSwitchResult:

@Override
public void onBrowserSwitchResult(int requestCode, BrowserSwitchResult result, @Nullable Uri returnUri) {
    switch (result) {
        case OK:
            // the browser switch returned data in the return uri
            break;
        case CANCELED:
            // the user canceled and returned to your app
            // return uri is null
            break;
        case ERROR:
            // there was an error browser switching
            // Some possible issues you may encounter are:
            // - There are no activities installed that can handle a URL.
            // - The integration is not setup correctly.
            break;
    }
}

License

Android Browser Switch is open source and available under the MIT license. See the LICENSE file for more info.

You can’t perform that action at this time.