强制关闭Chrome自定义标签

时间:2019-02-09 23:30:49

标签: chrome-custom-tabs android-customtabs

我想通过单击设备后退按钮来关闭chrome的“自定义”标签,但是仅在该“自定义”标签暂时没有历史记录时才可以使用;当剩余历史记录时,单击设备后退按钮只会将自定义选项卡后退到其历史记录中的页面。我是否可以强制自定义标签随时忽略历史记录并立即关闭自定义标签?

我为自定义标签尝试了其他选项,如下所示,但看来对我的目的没有帮助。我非常感谢您的想法。谢谢!

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

我当前的代码是

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));

1 个答案:

答案 0 :(得分:0)

我不认为当前有一个选项可以执行此操作,因为我们无法覆盖chrome自定义标签上的onBackPressed操作。实际上,最好在许多用例中采用这种方式。