They'll lay eggs in conditions that are unsuitable for them to continue living. It's basicly their defense to ensure their survival. If they are in ideal conditions, they give birth to live babies. If not, they lay eggs, and when the water returns to appropriate conditions the eggs will hatch and the cycle starts all over again.