Photon wont connect back to wifi

thanks @UMD, @ScruffR, @armor for your replies.

according to the link I need to add:
STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY));
but I am already using:
STARTUP(softap_set_application_page_handler(myPage, nullptr));
which I use to generate an access point if no wifi credentials are stored.
How do I proceed?

I only need to save one variable which is a counter of events, everything else can go. This variable gets updated every 20 sec aprox.
Normally when there is no wifi I am saving many other variables into an array:
#define ARRAYSIZE 1000
String backup[ARRAYSIZE];
in the hope that wifi comes back and I can read the array and publish the missing stuff. It works.

My only issue is that sometimes it wont reconnect and wont respond to any input, it is stuck forever, probably because of what @armor said, since my measurements do sometimes use delays for some seconds. I will add Wifi.ready() before asking if Particle.connected() and see if that helps.