MRK1000 WiFi freezeing when switching power source


I'm running a small IoT project with an ePaper screen and an Arduino MKR1000 connecting to an MQTT broker.

The system is mobile, so it runs on a battery. Frequently, whenever I plugin in a 5v source via USB or unplug it to let it continue to run on the 3.7v battery - the Arduino freezes and is inaccessible unless i let the 3.7v battery run out or reset the board.

The sketch stops running and USB connection cannot be established for reprogramming, i have to reset it.

Is there an explanation for this and can it be worked around?

Excuse me if i'm doing something wrong. I'm kind of new to the Arduino scene. (please let me know!)

Thanks in advance