3

我有一个活动 A,它启动自定义选项卡。我需要知道自定义选项卡打开时,任务(活动所属的)是进入后台还是进入前台。

我知道这个问题How to detect when an Android app go to the background and come back to the foreground。这个问题提到的解决方案对我不起作用,因为一旦启动自定义选项卡,就会收到 onbackground 回调,这不是我想要的。当包含活动 A 的任务进入后台时,我想要 onbackground 回调。

4

2 回答 2

1

使用CustomTabsCallback,您可以在选项卡变为隐藏(进入后台)时使用TAB_HIDDEN回调或TAB_SHOWN选项卡变为可见(进入前台)时的回调进行监听。

从文档:

TAB_HIDDEN

当标签被隐藏时发送。

TAB_SHOWN

Sent when the tab becomes visible.

下面是一个完整的工作示例,说明如何使用上述回调:

public class CustomTabsActivity extends AppCompatActivity {

    private CustomTabsServiceConnection mCustomTabsServiceConnection;
    private CustomTabsClient mCustomTabsClient;
    private CustomTabsSession mCustomTabsSession;
    private CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.customTabsButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showCustomTabs();
            }
        });
        initCustomTabs();
    }

    @Override
    protected void onStart() {
        super.onStart();
        CustomTabsClient.bindCustomTabsService(this, "com.android.chrome", mCustomTabsServiceConnection);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void initCustomTabs() {
        mCustomTabsServiceConnection = new CustomTabsServiceConnection()
        {
            @Override
            public void onCustomTabsServiceConnected(@NotNull ComponentName componentName, @NotNull CustomTabsClient customTabsClient)
            {
                mCustomTabsClient = customTabsClient;
                mCustomTabsClient.warmup(0L);
                mCustomTabsSession = mCustomTabsClient.newSession(new CustomTabsCallback()
                {
                    @Override
                    public void onNavigationEvent(int navigationEvent, Bundle extras) {
                        switch (navigationEvent)
                        {
                            case CustomTabsCallback.TAB_SHOWN:
                                //Sent when the tab becomes visible (goes into foreground)
                                break;
                            case CustomTabsCallback.TAB_HIDDEN:
                                //Sent when the tab becomes hidden (goes into background)
                                break;
                        }
                    }
                });
                builder.setSession(mCustomTabsSession);
            }
            @Override
            public void onServiceDisconnected(ComponentName name) {
                mCustomTabsClient = null;
            }
        };
        CustomTabsClient.bindCustomTabsService(this, "com.android.chrome", mCustomTabsServiceConnection);
    }

    private void showCustomTabs(){
        builder.setShowTitle(true);
        CustomTabsIntent customTabsIntent = builder.build();
        customTabsIntent.launchUrl(this, Uri.parse("https://stackoverflow.com/"));
    }
}
于 2021-12-20T11:02:10.697 回答
0

您的活动和 chrome 自定义选项卡之间的关系取决于启动模式。您可以在当前堆栈或新堆栈中启动自定义选项卡。

于 2021-12-17T02:47:00.943 回答