capacitor
capacitor copied to clipboard
[Android] networkStatusChange-event fires when app becomes active
Description of the problem: Every time app becomes active networkStatusChange is fired for some reason. Only tested on Android. Possibly also occuring on iOS.
Affected platform
- [x] Android
- [ ] iOS
- [ ] electron
- [ ] web
OS of the development machine
- [x] Windows
- [ ] macOS
- [ ] linux
Other information:
Capacitor version: Beta 22
Steps to reproduce: Network.addListener('networkStatusChange', (status) => { console.log('networkStatusChange was fired') })
Link to sample project:
Looking at the android code, it registers a handler onResume
, so likely this register is causing it to fire. Guessing iOS doesn't actually have the same pattern:
https://github.com/ionic-team/capacitor/blob/214b93b491382bc1bae5bf0842ca0b24b83cfa06/android/capacitor/src/main/java/com/getcapacitor/plugin/Network.java#L78
Hi, is there any fix or workarround for meaning while?
Having the same problem....every time the OS displays a native screen (file picker, permission request, alert, modal) and then the app comes back, the event is triggered. Not really ideal since in my app it resets back to the original page. Any workarounds or fixes?
Same here. Unfortunately I get a 'connected = false' as parameter value, when I wake up the device from standby after some minutes and the app is in focus. When I get 'connected = false' I show an offline-dialog that waits for a 'connected = true' till it disappears again. But there is no new event with 'connected = true' although I am online all the time. Is this maybe an Android bug?
Edit: Workaround for me for the correct status is something like this:
Network.addListener('networkStatusChange', () => {
setTimeout(async () => {
const status = await Network.getStatus();
}, 100);
});
Instead of using the parameter, I used 'getStatus' again. Although this seems to me to be very error-prone, I have no other idea to prevent the sporadically wrong value after app resume.
I'm using this as a work around, remove the listener on pause, add back in on resume.
private async init(): Promise<void> {
await this.platform.ready();
await this.setInitialValue();
this.addNetworkListener();
this.platformResumeSubscription = this.platform.resume.subscribe(async() => {
await this.setInitialValue();
this.addNetworkListener();
});
this.platformPauseSubscription = this.platform.pause.subscribe(() => {
this.networkListener.remove();
});
}
private addNetworkListener() {
this.networkListener = Network.addListener('networkStatusChange', (status) => {
this.updateNetworkStatus(status);
});
}
I have experienced the same issue. I have resolved it with a status comparison:
async subscribeToNetwork(): Promise<void> {
this.networkListener = Network.addListener('networkStatusChange', status => {
this.doMagic(this.networkStatus, status);
this.networkStatus = status;
});
this.networkStatus = await Network.getStatus();
}
private doMagic(oldStatus: ConnectionStatus, newStatus: ConnectionStatus): void {
if (oldStatus.connected && !newStatus.connected) {
// Do your magic
}
}
Having the same problem the android version is firing the event with every native event, do you an update ?
Any update regarding this ? , @mlynch I don't how this issue is low priority ?
same problem here.. causes lots of trouble in code design..
this is NOT a low priority issue.. please give this issue your attention!
Hey everyone, I have the same problem. I listen to networkStatusChange in the app to prevent the app of working when there's no internet. When the internet is coming back, I reload the app. Testing the app for the first time in Android Studio with the simulator, the app keeps on refreshing and in the console I can see this line:
Notifying listeners for event networkStatusChange
I don't understand what is causing these networkStatusChange ...
I disabled my system for the sake of testing, but I'd like to understand why there's these network change status being triggered randomly.
If anyone knows how to stop this from happening, it would be great.
Thanks in advance