Android Question Disable timers

davemorris

Active Member
Licensed User
Longtime User
Hi, Guys
I stumbled on a statement made in an old post by Erel, https://www.b4x.com/android/forum/threads/ispaused-activity-but-activity-is-null.73510/#content post #6
which said simply "Make sure that all your timers are declared as Process_Global variables and are disabled in Activity_Pause. "

I am aware that the use of timers in some OS can cause problems, but interested in what they damage the could do in B4A.
I have tried to search the community for some guidance without real success.

I tend to use timers all over the place in both activities, services and classes.
Question #1: Obviously in activities timers are declared in Process_Global sections, but in classes (used within service or activities) these timers have been declared in Class_Globals - Question, is this ok?

Question #2: I do intend to go thro' all my timers and ensure that code is added to the appropriate Activity_Pause() to disable these timers (a special call to the class should do it) - is this correct or is there a better way?

Also, when running debug I sometimes are unable to start the App without it report errors which can only be resolved by closing down the IDE and restarting it. I have tended to blame B4A, but I do now wonder if it is my code and how I handle timers.

I would be most grateful for some comments

Kind regards
Dave
 

LucaMs

Expert
Licensed User
Longtime User
Question #1: Obviously in activities timers are declared in Process_Global sections, but in classes (used within service or activities) these timers have been declared in Class_Globals - Question, is this ok?
Sure.

Question #2: I do intend to go thro' all my timers and ensure that code is added to the appropriate Activity_Pause() to disable these timers (a special call to the class should do it) - is this correct or is there a better way?
I have not understood exactly what you mean but if you have an instance of your class containing a timer, create a public method to stop the timer (in your class, obviously); eventually you will call this method from within Activity_Pause.

Also, when running debug I sometimes are unable to start the App without it report errors which can only be resolved by closing down the IDE and restarting it. I have tended to blame B4A, but I do now wonder if it is my code and how I handle timers.
Post a concrete example (logs); in some cases a "Clean projects" (CTRL+P) is needed.
 
Upvote 0

davemorris

Active Member
Licensed User
Longtime User
Hi
Thanks for quick response, looks like I was on the correct track.

Can I ask another question - Erel's original comment about timers suggests if you don't disable them it is bad practice - but what damage does it actually do and how can you spot it?

Regards
Dave
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Erel's original comment about timers suggests if you don't disable them it is bad practice - but what damage does it actually do and how can you spot it?
I'm not sure but this was an Erel's answer:
You should always pause the timer in Activity_Pause and result it in Activity_Resume. The timer will not tick when the activity is paused.

then I think Erel meant that you could assume that the timer continues to work even when the Activity is paused and therefore you may get different results than you expect.
 
Upvote 0
Top