While there are specific times to use both, I find that imperfect is used when there is no definite stopping place or if the action is a continuing thing in the past. While the preterit is a one time action- starts, stops.Â
Cuando hablaba, me estornudó.Â
When I was walking (it's just a continuing action in the past), I sneezed (happened and ended).Â
Cuando era un niño, mi mamá murió.Â
When I was a kid, my mom died.Â
Being a kid is the underlying action, 'died' is the singular action that began and ended. hope this helps :)