Write basic selected state save and restore logic.