Search code examples
androidandroid-lifecyclelifecyclechrome-custom-tabsandroid-customtabs

Callback when task goes into background or comes into foreground?


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.


Solution

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