Regarding the IDE, every keystroke you type gets recorded. That allows you to go "back in time" with Ctrl+Z a looooong way, even across sessions. That kinda makes us a restartable app, without the API. In the event of a crash, your program can always be restored by opening the IDE, hitting CTRL+Z and confirming you want to:
[ This attachment cannot be displayed inline in 'Print Page' view ]
Now for your own programs, you can detect the user hit the CLOSE button with the _EXIT function (http://www.qb64.org/wiki/EXIT_(function)), then you can perform any save routines before actually returning to SYSTEM (http://www.qb64.org/wiki/SYSTEM).