14

I have an activity 'A' and inside that activity, I have opened a chrome custom tab. Now when the user closes the chrome custom tab I want to open another activity 'B'. Is there a way to know when the chrome custom tabs has been closed. Or any other way to solve the above problem.

rahulk9
  • 795
  • 3
  • 10
  • 20
  • Please refer the below link, It is not a concrete solution but might help to some one. https://github.com/Adyen/adyen-android/issues/24 – ram Nov 12 '19 at 08:35

3 Answers3

42

In Activity A you launch the Chrome Custom Tab like this:

private final int CHROME_CUSTOM_TAB_REQUEST_CODE = 100;

public void launchCustomTabs(String url) {
    CustomTabsIntent customTabsIntent = builder.build();
    customTabsIntent.intent.setData(Uri.parse(url));
    startActivityForResult(customTabsIntent.intent, CHROME_CUSTOM_TAB_REQUEST_CODE);
}

And in onActivityResult your check for that request code:

if (requestCode == CHROME_CUSTOM_TAB_REQUEST_CODE) {
    startActivity(this, ActivityB.class);
}
Claus Holst
  • 901
  • 1
  • 8
  • 13
  • 3
    as for android 31 doesn't work onActivityResult is called as soon as the customtab open and is never called on its dismiss – Rafael Lima Oct 15 '21 at 18:33
14

You could keep track that Custom Tabs was opened on a boolean variable on Activity A.

private boolean mCustomTabsOpened = false;

public void launchCustomTabs(String url) {
   mCustomTabsOpened = true;
   new CustomTabs.Builder().build().launchUrl(this, Uri.parse(url));
}

Then, use Activity A's onResume() to launch Activity B

public void onResume() {
    if (mCustomTabsOpened) {
        mCustomTabsOpened = false;
        startActivity(this, ActivityB.class);
    }
}

You may want to use the KeepAliveService to prevent ActivityA from being destroyed, as illustrated here

andreban
  • 4,621
  • 1
  • 20
  • 49
  • 1
    I think this is the correct link (the file has changed since this was posted): https://github.com/GoogleChrome/custom-tabs-client/blob/a9f1a9b6cb4b52df31aba79813029afec4f570e0/Application/src/main/java/org/chromium/customtabsclient/MainActivity.java#L190 – Richard Marskell - Drackir Sep 22 '17 at 11:39
1

well, this doesn't work, because it's not possible as per now to track the closing of chrome custom tab, if you are trying to call or display a dialog box on hit of back button, i.e., to ask for a confirmation. Well you can handle them over your activity (which is launching it at the first place) but that's not what you want i think. But if anyone does find the solution then please comment below.

Siddharth Choudhary
  • 1,069
  • 1
  • 15
  • 20