向 Chrome 自定义选项卡关闭按钮添加监听器

问题描述 投票:0回答:5

我有一个 Chrome 自定义选项卡,但我想向标题栏左上角的关闭 (X) 按钮添加侦听器。

我想在用户每次点击关闭按钮时触发回调。

我能够在 Web 视图中执行此操作,但无法弄清楚 Chrome 自定义选项卡是否可以这样做。

这是我用来调用自定义选项卡的代码片段:

     CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
                    builder.enableUrlBarHiding();
                    builder.setToolbarColor(getTitleBarBackgroundColor());
                    builder.setStartAnimations(this, android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                    builder.setExitAnimations(this, android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                    customTabsIntent = builder.build();
                    customTabsIntent.intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY |  Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    customTabsIntent.launchUrl(this, Uri.parse(url));
android chrome-custom-tabs
5个回答
16
投票

已经有一些关于针对不同目的的 chrome 自定义选项卡关闭按钮自定义的查询(herehere)。从 Chrome 自定义选项卡的当前实现来看,不可能将侦听器直接添加到 chrome 自定义选项卡关闭按钮。您只能自定义其图标的关闭按钮

更新: 尽管您无法直接向 chrome 自定义选项卡关闭按钮添加侦听器,但您可以使用打开 chrome 自定义选项卡的调用者活动的 onResume()onActivityResult() 在关闭 chrome 自定义选项卡时触发回调。但请记住,在这种情况下,无论 chrome 自定义选项卡是通过关闭按钮还是设备后退键关闭,都会调用回调。


0
投票

无法覆盖关闭按钮的行为。但是,如果您只想触发回调,例如跟踪关闭按钮上的点击,则可以在启动自定义选项卡的 Activity 上使用

onResume()
回调,因为调用方 Activity 将由于以下原因而恢复:自定义选项卡正在关闭。


0
投票

有一个

setActionButton
方法允许您提供 PendingIntent,以便您可以有效地将其用作回调


0
投票

不可能。我不明白为什么“自定义选项卡”技术不支持它。对我来说,现在最好的方法是使用

onResume
。在开始自定义选项卡之前,我记得在 var/prop 中,自定义选项卡已打开,下次调用 onResume() 将检查存储的 var/prop,这很可能意味着“自定义选项卡已关闭”。


0
投票

这对我有用

ActivityResultLauncher<Intent> launcher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(), result ->{
                // TODO browser closed
            });;

CustomTabColorSchemeParams params = new CustomTabColorSchemeParams.Builder()
                    .build();

            CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder()
                    .setDefaultColorSchemeParams(params)
                    .build();
            customTabsIntent.intent.setData(uri);
            
            launcher.launch(customTabsIntent.intent);
© www.soinside.com 2019 - 2024. All rights reserved.