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.
Using the CustomTabsCallback you can listen when the Tab becomes hidden (goes into background) using the
TAB_HIDDENcallback orTAB_SHOWNcallback when the Tab becomes visible (goes into foreground).From the Documentation:
TAB_HIDDEN
TAB_SHOWN
Below is a full working example of how you can use the above callbacks: