Talking specifically about code, TDD really helped me, seriously, the red-green loop makes me comfortable to stop any time the tests are green, then I commit, which means that I also have very small commits which also help me with the interruptions once I'm back.
To not miss what I was thinking ahead, I always to keep a todo list (I just use Notes) of everything that comes to mind that I still need to fix or code which I postponed from the current commit, so I don't loose track while at the same time it frees me to focus on a specific thing at a time.
Now sometimes this does not apply, sometimes you just need long, deep and uninterrupted focus, like thinking about the perfect abstraction for something, or those long debugging sessions I mentioned. For those, I try to be sure to block some time or I don't even start it on that day