Auto save state before the emulator freezes

It's a known issue that when emulator is running on iOS and the device gets a call or the alarm rings the emulator freezes and the only thing you can do is force closing RetroArch and open it again. In this process the game state is not saved and the player loses all the progress.

I tried to fix this bug, but I lacked knowledge. I did a simple workaround to auto save the state when the application will change from active to inactive. Thereby the player's progress is not lost.