Vardaan Sharma
Vardaan Sharma

Reputation: 1165

Send Chrome custom tab to background

I invoke a Chrome custom tab from my activity, and i want to be able to go back to my app after a certain page is opened on my custom tab without having the user click on the "X" button.

To do that, i added an intent filter on my activity (the same activity that invoked the custom tab), and when that page is opened it redirects to that activity on my app.

Now the issue is, the custom tab stays on top. If I close my custom tab manually, I do see that the activity has received the response and is working as expected, but my activity does not come to the foreground.

Any ideas why this is happening, and any way to resolve this?

I am aware that as of now there is no way to CLOSE a custom tab programmatically. All i want to do is send the custom tab to the background and bring my activity to the top.

My custom tab invocation code:

CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder()
  .setShowTitle(true)
  .setToolbarColor(getToolBarColor())
  .setStartAnimations(this, android.R.anim.slide_in_left, android.R.anim.slide_out_right)
  .setExitAnimations(this, android.R.anim.slide_in_left, android.R.anim.slide_out_right)
  .build();

customTabsIntent.launchUrl(this, Uri.parse(url));

My activity manifest:

<activity
    android:name="com.xyz.sdk.SomeActivity"
    android:allowTaskReparenting="true"
    android:configChanges="keyboardHidden|orientation|keyboard|screenSize"
    android:launchMode="singleTask"
    android:noHistory="true"
    android:theme="@android:style/Theme.NoTitleBar">
    <intent-filter>
        <data android:scheme="someString-${applicationId}" />
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
    </intent-filter>
</activity>

Upvotes: 1

Views: 2031

Answers (1)

Nisalon
Nisalon

Reputation: 171

When detecting the page in your activity, try putting the activity on top (rather than the Chrome custom tab in the background) like this:

// Launch activity again
Intent intent = new Intent(this, A.class);
// Setting CLEAR_TOP ensures that all other activities on top of A will be finished
//  and setting SINGLE_TOP ensures that a new instance of A will not
//  be created (the existing instance will be reused and onNewIntent() will be called)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);

Upvotes: 1

Related Questions