Nougat changed the way it handles CONNECTIVITY_CHANGED intents (basically ignoring it, forcing devs to use the job scheduler) so this leaves me wondering:
If I have an app that is in the middle of retrieving some data (and I checked if the phone was online at the time I did the request but the user is on the move and the phone connects to a different wifi access point, for example) and it fails, how do I detect that the connection has been restored and I cam retry fetxhing the data?
So in this case my app is in the foreground, I think Chrome (for android) has a similar function.
Do I need to poll it myself? Or is there some event that is allowed at that time?
While Andromeda's answer could be used, that solution is not Google's intended choice. Your question was what to do when a connection is lost and you need to resume operation when network service returns.
While CONNECTIVITY_CHANGE technically works, it was always a bit of a hack for this specific need and it will stop working in Nougat as soon as your app goes in the background. What you should really use is a job scheduler API. Google has offered us many options with differing requirements and features.
JobScheduler was added in Lollipop and adds a scheduler which can wait for network connectivity to schedule a job. It can even depend on the kind of connection, checking for unmetered or non-roaming connections. This option has no backwards compatibility, but works without Google Play Services.
GcmNetworkManager is a direct port of the JobScheduler functionality to versions before Lollipop, but it requires Google Play Services. GcmNetworkManager has been mostly deprecated by Firebase Job Dispatcher.
Firebase JobDispatcher provides another means to schedule jobs for versions before Lollipop, which uses Google Play Services by default, but can be configured to not require this dependency.
I just edited this post to add that Google replaced all of the three previous job schedulers by WorkManager, which is better than the others in almost every way. You can set a required network type for your job to run. You can even chain jobs together one after another.
All of these options will satisfy your need in a battery-friendly manner and your jobs will still get scheduled even if the device wakes up briefly from Doze mode.
Here is more information about the various options with examples provided by Google:
https://developer.android.com/topic/performance/scheduling.html https://developer.android.com/topic/performance/background-optimization.html#sched-jobs