Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 352
Zakir Avatar asked Nov 19 '15 05:11

Zakir


People also ask

How do I disable Chrome custom Tabs?

Open Google application or do a search, click the hamburger in the top left and go into Settings. Go into Accounts and privacy. Turn off 'open web pages in app'. This appears to turn off chrome custom tabs.

How do I close a custom tab?

Let, you open chrome custom tab from "MainActivity" and there is a option menu item "Close" in chrome custom tab, and on "Close" menu item click you want to close chrome custom tab and to go back the "MainActivity", then you can do it by starting "MainActivity".

What is open Tabs in custom Tabs?

Custom Tabs is a browser feature, introduced by Chrome, that is now supported by most major browsers on Android. It gives apps more control over their web experience, and makes transitions between native and web content more seamless without having to resort to a WebView.


1 Answers

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

Let, you open chrome custom tab from "MainActivity" and there is a option menu item "Close" in chrome custom tab, and on "Close" menu item click you want to close 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 want 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 app may break on systems that don't have Chrome installed.
So please follow the Best Practices to launch chrome custom tab.

like image 119
mdrafiqulrabin Avatar answered Sep 27 '22 17:09

mdrafiqulrabin