Zakir
Zakir

Reputation: 1565

How to close chrome custom tabs

In my app I have opened a url via Chrome Custom Tab. We know that when user taps the device back button or custom back button Chrome Custom Tab will be closed. Is it possible to close the Chrome Custom Tab by programatically without user intervention.

Upvotes: 13

Views: 25108

Answers (5)

Save the value in preferences or data store and restart the activity, then check the saved value and open anything or perform some action

Code to restart the activity:

fun restart() {
    val intent = Intent(this, MainActivity::class.java)
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
    startActivity(intent)
    Runtime.getRuntime().exit(0)
}

Upvotes: 1

mdrafiqulrabin
mdrafiqulrabin

Reputation: 1697

There is no such support currently to close the Chrome custom tab programmatically. But you can close it by starting your previous activity from where you launched the Chrome custom tab if you want.

Let, you open the Chrome custom tab from "MainActivity" and there is an option menu item "Close" in the Chrome custom tab, and on the "Close" menu item click you want to close the Chrome custom tab and to go back the "MainActivity", then you can do it by starting "MainActivity". For this, set your activity launchMode as singleTask and then start your activity with FLAG_ACTIVITY_CLEAR_TOP when button is clicked.

Check my demo code for details, hope it will help someone who wants something like this.

AndroidManifest.xml :

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

<receiver
    android:name=".CustomTabReceiver"
    android:enabled="true" />

        

MainActivity.java :

    public class MainActivity extends Activity {
    public static String CHROME_PACKAGE_NAME = "com.android.chrome";
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
    }

    public void onClick(final View view) {
        switch (view.getId()) {
            case R.id.btnOpenChromeCustomTab:
                launchChromeCustomTab();
                break;
            default:
                return;
        }
    }

    private void launchChromeCustomTab() {
        Uri uri = Uri.parse("http://www.google.com/");
        Intent intent = new Intent(mContext, CustomTabReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);

        CustomTabsIntent.Builder customTabsBuilder = new CustomTabsIntent.Builder();
        customTabsBuilder.addMenuItem("Close", pendingIntent);
        CustomTabsIntent customTabsIntent = customTabsBuilder.build();
        customTabsIntent.intent.setPackage(CHROME_PACKAGE_NAME);
        customTabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        customTabsIntent.launchUrl(mContext, uri);
    }

}

CustomTabReceiver.java :

public class CustomTabReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            Intent myIntent = new Intent(context, MainActivity.class);
            myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(myIntent);
        }

    }

activity_main.xml:

<Button
    android:id="@+id/btnOpenChromeCustomTab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:onClick="onClick"
    android:text="Open Chrome Custom Tab" />

Note:
Make sure that updated Chrome has installed on device by explicit checking before testing this code. Because in this demo code chrome custom tab has opened by setting a hard-coded package to com.android.chrome and the app may break on systems that don't have Chrome installed.
So please follow the Best Practices to launch the Chrome custom tab.

Upvotes: 17

brandav
brandav

Reputation: 695

You could use a custom URI scheme to link back to your app. Just make sure to register your scheme using intent filters (see deep links). Then you can start the activity:

private void closeBrowserTab(Context context, String customUri) {
    Uri uri = Uri.parse(customUri);
    Intent appIntent = new Intent(Intent.ACTION_VIEW, uri);
    context.startActivity(appIntent);
}

Upvotes: 0

android:noHistory="true" use this line in manifest.xml in activity it is work perfectly

Upvotes: 1

Egor Pasko
Egor Pasko

Reputation: 516

No. User consent is needed to close the custom tabs view.

Upvotes: 3

Related Questions