This also happens to me, but I didn't pay any attention to it because I thought it was my code doing something wrong, and haven't had time to look into it yet.
Basically, what I have is this:
if keyboard "A" key is pressed AND array = 0 -> populate array
And if I change layouts, the array goes back to 0. It's also global.