Unfortunately, there is very little you can do to prevent the spring from becoming unsprung when attaching a new line. Most cases the, the line attaches to the center of the spool which requires you to pull it far enough out of the case to manipulate it, and this is typically when the spring uncoils.
The tools you will want handy are a set of needle nose pliers (the smaller thickness the needle nose, the better), a screw driver to remove the starter from the block, some scissors or an exacto to cut the new line and any other tool you feel might assist in the effort. It takes me about 15 minutes to rebuild them including recoiling the spring or rebuilding the spring if that is needed.
As for how to properly rewind the spring, that is somewhat of an art and is not easily explained. I typically wind the spring by hand until I have it as compact as I can get it. I then replace it in the starter casing. I the respool the pull start line and replace the spool. This is another tricky step as you have to get the spool into the casing in such a way as to properly engage the spring. Every manufacturer has a slightly different method of attaching the sprin to the spool and to the casing. At any rate, once done wind the spool a couple of times to put some tension on the spring, then feed the end of the line out of the casing and get the handle on. Then carefully re-install it and go.