Simply train yourself to abstain from something.
It can be very simple and small things, the only important point is to make it a habit.
There is no better way (all other things suggested can help, but if you are not able to do the above, nothing will save you...).