jenzz / RxAppState

[DEPRECATED] A simple Android library that monitors app state changes (background / foreground).
MIT License
195 stars 20 forks source link

Foreground Service confuses the logic #20

Open nateridderman opened 6 years ago

nateridderman commented 6 years ago

Thanks for contributing this library!

I have an application that launches a Foreground Service under certain circumstances. I need to know if one of the application's activities is visible to the user, independently of whether the foreground service is running or not. However, what I noticed is that I never receive a BACKGROUND callback when I background the application if the foreground service is running. Is this expected? Looking at your code, it seems that onTrimMemory is not getting called in this case.

jenzz commented 6 years ago

Hi @nateridderman,

That sounds like a shortcoming of the library. You are right, onTrimMemory() is not getting called when you back out of the app via the back button. It does get called when you background the app by pressing home or recent apps though. I presume the app only receives the onTrimMemory() callback with TRIM_MEMORY_UI_HIDDEN when all UI components of your app become hidden from the user which includes foreground services (the documentation is sadly quite sparse on this topic).

I don't have a fix for it right now, but I'm planning to release an update soon that will allow you to use the recently introduced ProcessLifecycleOwner to detect app state changes which should solve your problem (https://developer.android.com/reference/android/arch/lifecycle/ProcessLifecycleOwner.html).

I will leave this issue open for now in case anyone else has an idea how to make it work using ComponentCallbacks2.