Practice mindfulness. That is, when you realize you’ve strayed from the task at hand, say, “oh! I got distracted” and return to the task at hand. Better yet, come up with a mantra, such as “focus on the task,” and repeat it every time you realize you’ve been distracted. Eventually, you’ll recover from first moments of distraction much more quickly.