pwittchen / ReactiveNetwork

Android library listening network connection state and Internet connectivity with RxJava Observables
http://pwittchen.github.io/ReactiveNetwork/docs/RxJava2.x/
Apache License 2.0
2.53k stars 272 forks source link

Issue with fragments #86

Closed renegens closed 8 years ago

renegens commented 8 years ago

Hello,

Great work on this lib. I am using it in onResume with observeInternetConnectivity() and are creating my subscriptions in onStart. This creates although a problem that even if I have internet the boolean callback is triggered and is showing that the user has internet. Is there any other way except with an another boolean to track the state?

pwittchen commented 8 years ago

Actually, this problem is related to Activity and Fragment lifecycle. When Activity or Fragment are created or resumed, the subscription is usually started and then you'll get a notification. If you don't want to be notified every time when Activity or Fragment is resumed I see two solutions for now:

Regards, Piotr

renegens commented 8 years ago

Thanks for the reply. I went ahead and did it the second solution. I think it's more robust in case I need in the future to add something which has a scope of the whole app.

Edit: FYI I need to combine both shared pref and an event bus. The event bus is only working if the app is in the foreground and sticky events were not working correctly. So when the app is not in the foreground I check a shared pref boolean to set the connectivity indicator in the onResume method of every fragment.