Yeah going back and changing the law on the spot to get a desired result is dumb. And is a violation of due process (the 14th Amendment). I wish I could say it never happens. Maybe I can explain how it does.
1) Even though they aren't supposed to, the courts can reverse even binding precedents at any time they like, and it probably happens more often than legal scholars want to admit. Example: the very same
cases in my previous post. The first Supreme Court case as quoted above, ruled that saluting the flag and reciting the Pledge of Allegiance is mandatory, and that patriotism overrides the 1st Amendment.
Just 3 years later, it seems the
following happened. Immediately after the first decision, a lot of people got beat up. Some beaten up people sued again, and a lower court stated:
Ordinarily we would feel constrained to follow an unreversed decision of the Supreme Court of the United States, whether we agreed with it or not. … however ... we do not feel that it is incumbent upon us to accept it as binding authority. The Supreme Court followed by accepting the lower courts' defiance and reversed themselves.
So courts can reverse themselves and make up whatever law, whenever they want.
2) When decisions are reversed, or new interpretations of law are made, the courts generally do not say "Well it
used to be this way, so because we've changed the law as we considered your case, you get to have the old law applicable to you." Instead, it's tough cookies and the new law applies immediately. There are some instances where they might apply the "old law" on a one-time basis, when considering sovereign and qualified immunity (11th Amendment and Section 1983) but that is unfairly only allowed to go in the government's favor.