A lot of times it will happen when you are in the middle of another book. A book that has gone from pure pleasure to write to a kind of muddy slog.
And then a voice will whisper inside of you: "Your current book sucks. It's fat and constantly scratches its belly, and frankly, sometimes it smells. But I'm a new idea! I would make a wonderful book. I would practically write myself. "
Do not give into temptation. Do not divorce your current book to run off and hastily marry your new idea. Because one day you will wake up and you'll realize you are stuck in the same muddy slog, only now it's with your once shiny new idea.
Does that mean you should give up on your wonderful, sexy new idea?
No. But what you should do is make it your affair book. Yes, sneak off every now and then to write it. Write with passion. Leave when it starts suggesting you need to do the dishes or take out the garbage. Come back to it with presents of energy and excitement and insight. Repeat as necessary.
Two of the best books I've ever written were not under contract, and I really shouldn't have been writing them. But I snuck out every now and then to meet up with them secretly. And I'm so glad I did.