capacitor icon indicating copy to clipboard operation
capacitor copied to clipboard

[Android] networkStatusChange-event fires when app becomes active

Open solojuve1897 opened this issue 5 years ago • 10 comments

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:

solojuve1897 avatar May 12 '19 08:05 solojuve1897

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

mlynch avatar May 18 '19 23:05 mlynch

Hi, is there any fix or workarround for meaning while?

UlisesCeca avatar Jun 03 '19 14:06 UlisesCeca

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?

tavosansal avatar Aug 27 '19 02:08 tavosansal

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.

AE1NS avatar Nov 11 '20 12:11 AE1NS

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);
    });
  }

pete-mcwilliams avatar Dec 04 '20 18:12 pete-mcwilliams

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
    }
  }

HunSpeedi avatar Jan 06 '22 08:01 HunSpeedi

Having the same problem the android version is firing the event with every native event, do you an update ?

HugoLeBoennec avatar Jan 11 '24 15:01 HugoLeBoennec

Any update regarding this ? , @mlynch I don't how this issue is low priority ?

a7md3bdelfatta7 avatar Mar 19 '24 12:03 a7md3bdelfatta7

same problem here.. causes lots of trouble in code design..

this is NOT a low priority issue.. please give this issue your attention!

elazarza avatar May 09 '24 11:05 elazarza

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

undisconnected avatar Jul 25 '24 22:07 undisconnected