How to close chrome custom tabs

In my application, I opened the URL through the Chrome user tab. We know that when a user clicks the device’s back button or user’s back button, the Chrome user’s tab will be closed. Is it possible to close the Chrome user tab programmatically without user intervention.

+4
source share
2 answers

There is currently no such support to close user chrome tabs programmatically. But you can close it by starting the previous activity, from where you started the user chrome tab, if you want.

Let’s open the chrome user tab from “ MainActivity ”, and the “Chrome” tab has the “ Close ” and “ Close ” tab, you can close the chrome user tab and go back to “ MainActivity ”, then you can do this by running “ MainActivity " To do this, set the launchMode action as , and then start your activity with a click of a button. singleTask FLAG_ACTIVITY_CLEAR_TOP

Check out my demo code for details, hope it helps 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" />

:
, Chrome . chrome , com.android.chrome, , Chrome.
, , Best Practices, chrome custom.

+7

. , .

+5

Source: https://habr.com/ru/post/1616472/


All Articles