chrome-custom-tabsandroid-customtabs

Forcing to Close Chrome Custom Tab


I want to close a chrome Custom Tab with clicking device back button, but it only works when the custom tab has no history for the moment; when there's a remaining history, clicking the device back button only rewinds the custom tab back to a page in its history. Can I force the custom tab to ignore the history at any point and close the custom tab immediately?

I tried different options for the custom tabs such as below, but it appears none helps with my purpose. I would hugely appreciate your idea. Thank you!

customTabsIntent.intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
customTabsIntent.intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
customTabsIntent.intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

My current code is

String url = eacharticle.get("url");
String PACKAGE_NAME = "com.android.chrome";
android.support.customtabs.CustomTabsIntent customTabsIntent = new android.support.customtabs.CustomTabsIntent.Builder().setShowTitle(true).build();
customTabsIntent.intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
customTabsIntent.intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
customTabsIntent.intent.setData(Uri.parse(url));
java.util.List<android.content.pm.ResolveInfo> resolveInfoList = context.getPackageManager().queryIntentActivities(customTabsIntent.intent, android.content.pm.PackageManager.MATCH_DEFAULT_ONLY);
for (android.content.pm.ResolveInfo resolveInfo : resolveInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
if(PACKAGE_NAME.equals(packageName))
customTabsIntent.intent.setPackage(PACKAGE_NAME); // force use chrome if installed
}
customTabsIntent.launchUrl(context, Uri.parse(url));

Solution

  • I dont think there is currently an option to do this since we cannot override onBackPressed action on chrome custom tabs. Actually it is better to work this way in many use case.