I can relate to the pattern you describe. I know that many others here can as well.
Where are you holding with this now?
Loneliness, boredom, resentment, frustration, stress, and many other emotions can trigger us to seek our drug. If we can recognize this, the next time we're in the situation, we can take extra measures to avoid being alone, and, even better, work on ourselves and our lives so these emotions don't rule us.