I'm not sure what makes it to happen and android studio is far the worst IDE i've worked with (how can a tool dare to edit and save the user files without even ask? the usability team of google deserve a nobel)
But this is what I do to manage solve it...
As soon as you identify an event like this happened close android studio, stop any build in progress and dont try to run the project...
it seams that sometimes the modifications aren't done in the real source file, but android studio is opening some intermediate files instead of the original sources and showing it to you when you open the source... some kind of symbolic link mess
So with android studio closed open the file explorer go to your workspace and create a copy of your project in another directory (as far of android studio as possible)
open your souce files with any text editor and check if they are still okay...
re-open android studio and copy/paste file by file, the contents of the copy project you made to the official project
It is super boring and may take a while depeding of your project size, but i managed to recover over 90% of my files like this