I think how soon after may matter more than whether or not after.
Some banks (Chase) are known for looking for patterns of "seeking rewards only" on the cards you have, or used to have, with them, when you go to apply for another card. So using the card a little bit at non-retention times, and then cancelling further away from the retention offer, may help with such banks.
Ie, you tried to ask it as a bank-independent question, but it's not completely bank independent. At least at this snapshot in time, some banks care very little about when you cancel cards (the people who applied for Citi Exec AA cards, did the gigantic spend in just a few days, got the bonus posted, and cancelled before the first year AF even hit, and then repeated that multiple times, shows how little Citi cared about that, at least back then), and some banks care much more.
If you want to have a one-policy-fits-all-banks approach, therefore, you have to design each element of it according to the worst-case bank.
Btw, when I cancelled some cards recently after (1) paying an AF for the second year, and (2) getting a retention bonus for it, I got a prorated credit of the AF back when I cancelled later in the year. So you don't necessarily lose it all by waiting, just a part of it. But you may gain "better marks" at the bank by waiting, while risk getting "blacklisted" if you try to push things by doing stuff like getting a retention bonus then cancelling fast enough to get the AF fully refunded anyway.