Sorting out Activity_Create & Resume/Pause.

nfordbscndrd

Well-Known Member
Licensed User
I've finally reached the point in the development of my app where I need to tackle Resume/Pause, etc. My first attempt involved saving program data in Pause and Restoring it in Resume, but that caused restoring when the program hadn't gone away. So as a last resort, I read the documentation.
:sign0012:

So now I've read the tutorials and related info again, but I'm still not sure I have everything straight:

1. When I turn on my device and run my app, Sub Activity_Create runs (with FirstTime = True) followed by Sub Resume running. If I have run the app before and data was saved (game scores, options selected, etc.), then I read that data back in (in Activity_Create or a sub referenced therein) and use it to continue the game.

2. While running my app, which has a single Activity, I press Home (at which point Sub Pause is executed and the game's data are saved to disk) and run some other apps. I return shortly to my app (which Android still has in memory). Resume will run but Activity_Create will not. The app's screen and data will be just where it was and the app will continue as if I had never left. So although app's settings were saved in Pause, I don't have to restore them.

3. Like #2, but I take longer running apps and by the time I get back to my app, it is no longer in memory, so Activity_Create runs (with FirstTime = False, not that I'm using it) and reloads the saved game data to restore the game's scores and settings, then Resume runs.

4. My app runs landscape only, but if both orientations were allowed and the device were rotated, the results would be the same as #3.

5. If I put the device into Sleep mode and later resume my app, then Resume will run but Activity_Create will not and the game will continue where I left off.

6. If I'm running the app and power off the device, Pause will run and save the app's data while the device is powering down, and next time I run the app will be as in #1.

I'm not using the FirstTime flag at all, and only using Resume to enable the timers which were disabled in Pause.

Have I got all this straight?

One possible glitch is that if the user pulls out the SD card, then powers down, the data can't be saved and in Pause, there's nothing that can be done about it (like telling him to put it back in). Right?
 

Erel

Administrator
Staff member
Licensed User
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).
3. This can also happen if the user pressed on the back key, causing your activity to be destroyed.
4. That is correct.
5. Unless the activity was destroyed by the OS.
6. Correct.

FirstTime is usually important. It allows you to initialize variables that are not affected by the activity life cycle.

You can write your applications settings to the internal folder. It should be a small file.
 

nfordbscndrd

Well-Known Member
Licensed User
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?
 

Erel

Administrator
Staff member
Licensed User
I will try to give you some practical tips, assuming that your application state is important:
- The state should always be saved to a file / db in Activity_Pause.
- Creating the layout should always be done in Activity_Create (FirstTime doesn't matter).
- Loading the state from a file should be done in Activity_Create. If FirstTime = False you can optimize your application and load the state from a process global variable instead of a file.

You are correct that nothing happens to your program while it is paused and then resumed (without being destroyed in the middle).
Activity_Resume is important for resuming elements like timers, animations, GPS, camera and so on.
 
Top