1. FirstTime = True when the process running your program is created. It can happen after you turn on the phone or when your program resumes from the background and its process was killed (by the OS or a task manager).
That statement worries me because it calls into question my assumptions about what's going on. I thought that what I call an app's being "removed from memory" (to make room in memory for some other app) was synonymous with what you call its being "destroyed", but I don't think I've heard it referred to before as "its process was killed" and that the app "resumes from the background", which sounds like it is still in memory.
For the rest of this post, however, I'm going to assume that in the above case, the app, for all practical purposes, doesn't so much "resume" as "starts over", since it reverts to the same state as it usually is at the start of the app (prior to loading and restoring saved data).
5. Unless the activity was destroyed by the OS [in Sleep mode].
I thought the app would be safe in sleep mode because nothing NEW can be run at that time that would cause the OS to have to make room in memory by destroying the app. Is that not correct? (Why do I have a feeling that's NOT correct?) If you left the app before putting it into Sleep, then that falls into a different category; otherwise, it comes out of Sleep back to your app as you left it, right?
FirstTime is usually important. It allows you to initialize variables that are not affected by the activity life cycle.
By "activity life cycle", I assume that you mean when the OS has removed the app from memory, which was every case but 2 and possibly 5, because when the app is NOT removed from memory, Activity_Create doesn't run again when the app resumes and, in fact, there is no need to reload the saved data because it's still in memory and the app just resumes where it left off automatically. (Assume I'm saying "Right?" after every statement I make since I'm not talking with any great assurance.)
When the app IS destroyed in memory, including because of the device's being powered down, Activity_Create will run and I will ALWAYS want it to load the saved program data such as, in my app's case, deal number number, dealer, scores, etc., so that the game can continue. In such a case, FirstTime is irrelevant because either the app is gone from memory and Activity_Create is running and reloading saved data every time, or it is still in memory, in which case Activity_Create is NOT running but the data is still in memory. However, I understand that some types of apps may or may not want to reload data and thus will need to check FirstTime's status.
If the user was in the middle of a deal when he left and the app was removed from memory, I don't worry about trying to get back to the exact point where he left, I just start a new deal. The reason for that is normally, he will have been away long enough to not remember what had happened in the deal, so it's better for him to start the deal over.
This wouldn't be true if he had just changed orientations, but my app is locked in landscape. And it wouldn't be true if he pressed Back for some reason (instead of Home) and he restarted the app, but he shouldn't be doing that anyway (maybe he'll learn).
While doing some more searching on the topic, I just came across
a post I hadn't seen before which says:
Activity_Resume is the best place to restore stuff. You can get an Activity_Pause followed by an Activity_Resume without going through an Activity_Create, for example if the user presses the Home key and then restarts your app.
The above statement was by
agraham and was agreed to
klaus, the two leading authorities on B4A other than yourself, so I'm really sticking my neck out here:
My testing shows that if you press Home and "restart" your app before the OS has needed to remove it from memory (my #2), then you are not "restarting" it. You are just continuing it where you left off as if you had never left, thus no "restoring stuff" is needed, so it would be a waste of time to restore the data in Activity_Resume. Even if you press the app's icon, the OS will not remove the app from memory and then start it from scratch, it will just resume the one that is already in memory.
However, if you press Home and stay away long enough that the OS has removed it from memory (my #3) or if you press Back instead of Home, then you *are* restarting it and Activity_Create WILL run (and the data can be restored) before Activity_Resume, so once again, there is no reason to restore the data in Activity_Resume. (Again: the implied: "Right?")
You can write your applications settings to the internal folder. It should be a small file.
Somehow I had got to thinking that if the device is powered down, the file in the internal folder would be killed, but now I can't find any support for that. Maybe I was getting it mixed up with DirInternalCache. So instead of writing to DirDefaultExternal, I should be writing to DirInternal, right?