关闭Chrome自定义选项卡的回调
问题描述:
我有一个活动“A”,并在该活动中打开了一个Chrome自定义选项卡。现在,当用户关闭Chrome自定义选项卡时,我想打开另一个活动“B”。有没有办法知道chrome自定义选项卡何时关闭。或者任何其他方式来解决上述问题。关闭Chrome自定义选项卡的回调
答
你可以保持跟踪自定义选项卡被打开了一个布尔变量上的活动A.
private boolean mCustomTabsOpened = false;
public void launchCustomTabs(String url) {
mCustomTabsOpened = true;
new CustomTabs.Builder().build().launchUrl(this, Uri.parse(url));
}
然后,使用活动A的onResume()
推出活动B
public void onResume() {
if (mCustomTabsOpened) {
mCustomTabsOpened = false;
startActivity(this, ActivityB.class);
}
}
您可能需要使用KeepAliveService以防止ActivityA被破坏,如图所示here
答
在活动A中,您启动Chrome自定义标签是这样的:
private final int CHROME_CUSTOM_TAB_REQUEST_CODE = 100;
public void launchCustomTabs(String url) {
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.intent.setData(Uri.parse(url));
startActivityForResult(customTabsIntent.intent, CHROME_CUSTOM_TAB_REQUEST_CODE);
}
而且在onActivityResult您对此请求的代码检查:
if (requestCode == CHROME_CUSTOM_TAB_REQUEST_CODE) {
startActivity(this, ActivityB.class);
}
答
好,这是不行的,因为它是不可能的,因为每次我们跟踪Chrome定制闭幕标签,如果您试图在返回按钮命中时调用或显示对话框,即要求确认。 那么你可以通过你的活动处理它们(首先启动它),但那不是你想要的。 但如果有人找到解决方案,请在下面评论。
谢谢@andreban,它解决了我的问题。 – princerk
我认为这是正确的链接(该文件发布后发生了更改):https://github.com/GoogleChrome/custom-tabs-client/blob/a9f1a9b6cb4b52df31aba79813029afec4f570e0/Application/src/main/java/org/chromium /customtabsclient/MainActivity.java#L190 –