4

I have an activity A, it launches custom-tab. I need to know while the custom tab is open, if the task (of which the activity is part of) goes to background or comes to foreground.

I am aware of this question How to detect when an Android app goes to the background and come back to the foreground . The solutions mentioned for this question don't work for me because as soon as custom tab is launched, the onbackground callback is received, which is not what I want. I want onbackground callback, when the task containing the activity A goes to background.

q126y
  • 1,589
  • 4
  • 18
  • 50

2 Answers2

1

Using the CustomTabsCallback you can listen when the Tab becomes hidden (goes into background) using the TAB_HIDDEN callback or TAB_SHOWN callback when the Tab becomes visible (goes into foreground).

From the Documentation:

TAB_HIDDEN

Sent when the tab becomes hidden.

TAB_SHOWN

Sent when the tab becomes visible.

Below is a full working example of how you can use the above callbacks:

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/"));
    }
}
MariosP
  • 8,300
  • 1
  • 9
  • 30
  • 1
    This is promising, but the `onNavigationEvent` callback is not called if we use firefox as custom tab provider(and use `org.mozilla.firefox` to bind). – q126y Dec 21 '21 at 09:00
  • Yes unfortunately this is an open issue for FireFox which is mentioned here https://github.com/mozilla-mobile/focus-android/issues/2183 Hope to resolve this soon. – MariosP Dec 21 '21 at 09:16
  • Ah, so it if firefox issue. Can we use application context in `CustomTabsClient.bindCustomTabsService` and use it across activities? – q126y Dec 21 '21 at 09:37
  • Of course you can use getApplicationContext() and the callbacks will be invoked normally. – MariosP Dec 21 '21 at 09:44
0

The relationship between your activity and chrome custom tabs depends on the launchMode. You can launch the custom tab in current stack or a new stack.

Krahmal
  • 195
  • 13