【问题标题】:Callback when task goes into background or comes into foreground?任务进入后台或进入前台时回调?
【发布时间】:2021-12-27 07:13:13
【问题描述】:

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

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

【问题讨论】:

标签: android android-lifecycle lifecycle chrome-custom-tabs android-customtabs


【解决方案1】:

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

来自文档:

TAB_HIDDEN

标签隐藏时发送。

TAB_SHOWN

标签变为可见时发送。

以下是如何使用上述回调的完整工作示例:

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/"));
    }
}

【讨论】:

  • 这是有希望的,但是如果我们使用 firefox 作为自定义选项卡提供程序(并使用 org.mozilla.firefox 绑定),则不会调用 onNavigationEvent 回调。
  • 是的,不幸的是,这是 FireFox 的一个未解决问题,这里提到了 github.com/mozilla-mobile/focus-android/issues/2183 希望尽快解决这个问题。
  • 啊,如果是火狐的问题。我们可以在CustomTabsClient.bindCustomTabsService 中使用应用程序上下文并跨活动使用它吗?
  • 当然可以使用getApplicationContext(),回调会正常调用。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    • 2014-02-16
    • 2011-05-22
    相关资源
    最近更新 更多