When bathing, showering, or in a mikvah, one does not need to wear a yarmulke. However, the yarmulke should be the last to be removed and first to be put back on afterwards. Similarly, at the beach or swimming pool while in the water one does not need to wear a yarmulke, however, outside the water on the sand it is preferable for one to wear a yarmulke. While one is not wearing a yarmulke even in the above places that he doesn’t need to, he can’t recite a bracha or speak in learning.
Even if one is found in a place that he should not be, and wearing a yarmulke may cause a chilul Hashem, he must wear his yarmulke and not remove it.