7

I'm trying to incorporate Chrome Custom Tabs in my app and I'm having difficulty getting the exit animation working. I'm following the docs found here:
Link

The code I'm using is like the following:

String EXTRA_CUSTOM_TABS_EXIT_ANIMATION_BUNDLE = "android.support.customtabs.extra.EXIT_ANIMATION_BUNDLE";
Bundle finishBundle = ActivityOptions.makeCustomAnimation(mActivity, android.R.anim.slide_in_left, android.R.anim.slide_out_right).toBundle();
i.putExtra(EXTRA_CUSTOM_TABS_EXIT_ANIMATION_BUNDLE, finishBundle);

Bundle startBundle = ActivityOptions.makeCustomAnimation(mActivity, R.anim.slide_in_right, R.anim.slide_out_left).toBundle();
mActivity.startActivity(i, startBundle);

The tab launches with the desired animations but finishes with the default activity animation. Any ideas?

MattWilliams89
  • 157
  • 1
  • 15

2 Answers2

1

The recommended way to integrate your app with Custom Tabs is to use the Android Support Library.

To use it, add com.android.support:customtabs:23.0.0 as a compile dependency to your build.gradle.

Then, to set the exit animations and start the Custom Tab, do:

    CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder()
            .setExitAnimations(this,
                    android.R.anim.slide_in_left, android.R.anim.slide_out_right)
            .build();
    customTabsIntent.launchUrl(this, Uri.parse("http://www.example.com"));

Check the demos module in the GitHub sample for more details on how to use it with the Android Support Library.

To open it without the Support Library, you have to make sure you are setting the session extra. The code below will open a Custom Tab and properly set the exit animation.

public static final String EXTRA_EXIT_ANIMATION_BUNDLE =
        "android.support.customtabs.extra.EXIT_ANIMATION_BUNDLE";

public static final String EXTRA_SESSION = "android.support.customtabs.extra.SESSION";

public void openCustomTab() {
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));

    Bundle bundle = ActivityOptions
            .makeCustomAnimation(
                    this, android.R.anim.slide_in_left, android.R.anim.slide_out_right)
            .toBundle();

    Bundle extrasBundle = new Bundle();
    extrasBundle.putBinder(EXTRA_SESSION, null);
    intent.putExtras(extrasBundle);

    intent.putExtra(EXTRA_EXIT_ANIMATION_BUNDLE, bundle);
    startActivity(intent);
}

Hope to have helped.

andreban
  • 4,621
  • 1
  • 20
  • 49
  • I wasn't aware of the support library approach. I've implemented that now but I still have the same issue. My code is as follows: – MattWilliams89 Sep 03 '15 at 16:31
  • `CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder() .setStartAnimations(mActivity.getApplicationContext(), R.anim.slide_in_right, R.anim.slide_out_left) .setExitAnimations(mActivity.getApplicationContext(), R.anim.slide_in_left, R.anim.slide_out_right) .setToolbarColor(color) .build(); customTabsIntent.launchUrl(mActivity, Uri.parse(url));` – MattWilliams89 Sep 03 '15 at 16:31
  • Can you confirm that setting the Toolbar color is working? Also, are the animation files the same ones from the demo repo? – andreban Sep 03 '15 at 17:06
  • Yep toolbar color is working and the animations are the same as in the demo repo. I've tried using android.R.... Anim files too but with no luck. I wonder if its something to do with the context I'm giving the exit animation? – MattWilliams89 Sep 03 '15 at 19:03
  • I am also having the same issue, Enter animations are working, but the Exit animations don't. – Neal Sanche Sep 04 '15 at 17:09
  • 1
    I just tried it on a Lollipop device and it worked fine. The Marshmallow device seems like the one with the issue. – Neal Sanche Sep 04 '15 at 17:21
  • Android 7.1.1 running a 2 week old chromium build: Did it like in @MattWilliams89 s comment. BUT i got a FC when i only have the ExitAnimation. With StartAnimantion it opens up, but there are no custom animations at all. Did anyone find a solution for that? – WuerfelDev Mar 04 '17 at 23:51
0

I stumbled upon the same issue, enter animation working like a charm but couldn't get it working for the exit animations until I realised I might have passed the wrong context to setExitAnimations. Therefore be sure to pass the context of activity from where the custom tab is opened.