@TeeroyLyon
When you state the "code in your default function" - are you really stating the formula that you have in your Items property of the Gallery?
If so, this is very common if your formula contains any functions that are "data altering or shaping". So, using functions like AddColumn, GroupBy, etc. or even function for sorting will cause the gallery to have to re-evaluate its data when you alter the base datasource of the gallery.
There are a couple of options.
1) Make use of the Default property of the Gallery to reassign the last selected record in the Gallery. This is usually a bit complicated if you have a fairly extensive Items property on the gallery as the record you specify in the Default must match EXACTLY the record that would be found in the Items property.
2) Isolate the datasource into a snapshot variable for the Item property. This has the negative that your Gallery data will not be current as it will be based on the snapshot variable.
Either one will work, but which one you do is more dependent on your app design and needs.
Incidentally, this effect ONLY occurs if you have a data altering formula in the Items property. If you have a simple formula in items, this effect will not happen.
I hope this is helpful for you.