19

I am trying to implement an OAuth2 flow with an Android Chrome Custom Tab but my app is always closed (no crash) when the Chrome Custom Tab is receiving the 302 with the location/scheme of my app.

If I create a HTML page with ahref link and touch manually on it the Chrome Custom Tab is correctly switching to my app.

Seems like when handling the server 302 redirect in the Chrome Custom Tab it will not correctly handle my custom app scheme... but why?

If I try the same redirect URL in a stock browser or with a WebView everything is working too.

Here is my current setup:

MainActiviy.java

    Button btnChromeCustomTab = (Button) findViewById(R.id.btnChromeCustomTab);
    btnChromeCustomTab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder().build();
            String packageName = CustomTabsHelper.getPackageNameToUse(MainActivity.this);
            customTabsIntent.intent.setPackage(packageName);
            Uri theLocationUri = Uri.parse(URL);
            customTabsIntent.launchUrl(MainActivity.this, theLocationUri);
        }
    });

AndroidManifest.xml

    <activity android:name=".MainActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <intent-filter android:label="@string/filter_title">
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="myappscheme" android:host="oauth" />
        </intent-filter>
    </activity>

This is the redirect URL that the app received with HTTP 302 code:

myappscheme://oauth?code=1234567&state=tokenCheck123

build.gradle

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
    applicationId "de.myapptest.webviewtest"
    minSdkVersion 16
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}

dependencies {
   compile fileTree(dir: 'libs', include: ['*.jar'])
   testCompile 'junit:junit:4.12'
   compile 'com.android.support:appcompat-v7:23.2.1'
   compile 'com.android.support:design:23.2.1'
   compile 'com.android.support:customtabs:23.0.0+'
}

Thanks for any help...

sysbeast
  • 275
  • 1
  • 2
  • 9
  • 1
    From what I understood, your app is resumed and then closed, is that correct? Is there any relevant message on logcat? Also, could you post the MainActivity.java code? – andreban Mar 24 '16 at 12:19
  • Just for the record, I just tested redirect (302) to custom scheme `(myappscheme://...)` with a webview and it doesn't work. It says "unknown scheme". – User Feb 07 '20 at 07:51

4 Answers4

12

I've also observed my Android app unexpectedly background after server-side 302 redirection to a custom scheme, and observed expected handling from stand-alone Chrome and manually triggered redirection in the client.

I was able to "fix" the issue by calling the warmup function before loading the url that redirects.

In other words, this works:

void launchTab(Context context, Uri uri){
    final CustomTabsServiceConnection connection = new CustomTabsServiceConnection() {
        @Override
        public void onCustomTabsServiceConnected(ComponentName componentName, CustomTabsClient client) {
            final CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
            final CustomTabsIntent intent = builder.build();
            client.warmup(0L); // This prevents backgrounding after redirection
            intent.launchUrl(context, uri);
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {}
    };
    CustomTabsClient.bindCustomTabsService(context, "com.android.chrome", connection);
}

This doesn't work:

void launchTab(Context context, Uri uri){
    final CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    final CustomTabsIntent intent = builder.build();
    intent.launchUrl(context, uri);
}

The Chrome Custom Tab docs describe warming up as a best practice, but it also appears to help ensure expected behavior.

In terms of env, I'm testing on a Nexus 5X w Chrome 51. My chrome tab dependency in Gradle looks like this:

dependencies {
    compile 'com.android.support:customtabs:24.0.0'
Erik
  • 136
  • 3
  • 6
  • 2
    I already have client.warmup(0L) and my app still does that same thing. Any other ideas? – scoleman2272 Sep 23 '16 at 15:37
  • 1
    what package name should I gave instead of "com.android.chrome". Should I give mine app package name? – Ajinkya Jul 06 '17 at 17:45
  • @Ajinkya, that package name is the name of the package Chrome Custom Tabs package that will be instantiated (e.g. `com.android.chrome`, `com.browser.that.support.customTabs`.). So no, don’t put your package name there. – Sakiboy Feb 08 '18 at 01:44
  • that is interesting, how it works – walkmn Aug 18 '22 at 10:16
8

If I used android:launchMode="singleInstance" there were multiple instances in the task manager so this was no option.

Starting the CustomTabsIntent with FLAG_ACTIVITY_NEW_TASK Flag did the trick.

CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent intent = builder.build();

intent.intent.setFlags(FLAG_ACTIVITY_NEW_TASK);
intent.launchUrl(context, Uri.parse(url));
Sebastian
  • 143
  • 1
  • 8
0

It helped me to set the Activity that I use to start a CustomTab to singleInstance mode in the manifest file:

    <activity
        android:launchMode="singleInstance"
        android:name="com.example.SingleInstanceActivityToStartCustomTab"
    </activity>

And in the code I do as usual:

    CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    final CustomTabsIntent customTabsIntent = builder.build();
    customTabsIntent.intent.setPackage(someChromePackage);
    customTabsIntent.launchUrl(singleInstanceModeActivity, someUriThatDoesRedirect);

I tried warming up Chrome and even calling customTabsIntent.launchUrl() with some delay after calling client.warmup(0l); and neither helped.

dkunin
  • 60
  • 6
0

I'm pretty sure this is the result of a bug in Chrome. I updated all of my devices (GS6, GS7, Nexus 7 and Nexus 9) to the latest version of Chrome and my app is no longer minimized when the redirect occurs.

I just discovered this today (11/3/2016) so I don't yet have any information about the specific bug or its subsequent resolution. This is just what I've noticed.

Hope that helps!

bstar55
  • 3,542
  • 3
  • 20
  • 24