0

I want to detect when the user exits my app either by clicking the Back button or the Home button or the recents button. The solution mentioned in the link below works but it depends on callbacks from Activity Lifecycle.

After the introduction of chrome tabs on android a chrome instance is opened as a part of the app but for that, we don't get any callback for activity lifecycle as it is not a part of my application. Any workaround for this?

Find the solution based on ActivityLifeCycleCallback here:

How do I detect if the user has left my app?

bpr10
  • 1,086
  • 8
  • 14
  • as far as i know whenever a user leaves the application this will hit the onPause method so you can add some functionality there to detect if its gone. ref: https://developer.android.com/guide/components/activities/activity-lifecycle.html#onpause – Yamen Nassif Oct 09 '17 at 11:47
  • You are correct. The solution I mentioned is based on onPause and onResume of activities. But for chrome-tabs the activity lifecycle doesn't work as you don't get any callback for those. – bpr10 Oct 09 '17 at 11:49
  • 1
    "Any workaround for this?" -- I doubt it. – CommonsWare Oct 09 '17 at 11:50
  • @bpr10 i guess i got what you mean now, can you provide the code you are using to lunch the chrom-tab(s) – Yamen Nassif Oct 09 '17 at 11:56
  • You can use a broadcast receiver to detect the chrome launch. Check this answer. https://stackoverflow.com/questions/8061179/broadcast-receiver-to-detect-application-start – Sushin Pv Oct 09 '17 at 11:59
  • `onResume` / `onPause` + `onWindowFocusChange` might help – rupps Oct 09 '17 at 12:04
  • @SushinPv This can also be valid for a situation where user minimis]zes the app and launch chrome separately. For that case also it'll end up treating the app as foreground but it's still in the background. – bpr10 Oct 09 '17 at 12:06
  • @rupps the callbacks and onWindowFocusChange doesn't work for chrome-tabs. – bpr10 Oct 09 '17 at 12:07
  • Then I'd suggest to use a custom WebView. I haven't experience with Chrome Tabs, but I'm surprised OnWindowFocusChange doesn't get called when, well, window focus changes! – rupps Oct 09 '17 at 12:11
  • @rupps using a custom WebView is not an option as chrome-tabs support a lot more features than WebViews. So I can't-do away with chrome tabs. Also, OnWindowFocusChange won't get called for chrome-tab because chrome-tab window isn't really part of the app. So you'll get OnWindowFocusChange as false in the last opened activity and treat it as a minimize but the app is still open. – bpr10 Oct 10 '17 at 09:04
  • @bpr sorry to hear that, but i'm still impressed your activity doesn't get any lifecycle callbacks at all. WindowFocusChange for example should be called with false when your activity window loses focus, something that is definitely happening, isn't it? – rupps Oct 10 '17 at 22:07
  • @rupps My Activity does get WindowFocusChange with false. But when you move to a chrome tab from an activity the activity will lose focus and no other activity will gain focus as no other is in foreground but the chrome-tab is and for the user the chrome-tab feels like part of the app and the app is not minimized. In this case also we'll treat this as minimize but for the user it's not. – bpr10 Oct 11 '17 at 07:32

0 Answers0