1. When I had opened the door, children ran in.
2. When I had written my letter, I did some gardening.
In the first sentence the usage of past perfect is not allegedly necessary. Why? Both sentences after all contain two actions one of which precedes the other. Is that so that in the case of the first sentence opening the door and running in come immediately after?