Android Question Working With Timers

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am using a timer in one of many activity's in my project.

I have declared this timer in Process Globals and Initializing it in the Activity_Create sub like below:

B4X:
Sub Process_Globals
		Dim MyTimer As Timer
End Sub

Sub Activity_Create(FirstTime As Boolean)
	If MyTimer .IsInitialized = False Then 
		MyTimer .Initialize("MyTimer ",10)
	End If
	MyTimer .Enabled = True
End Sub

Sub MyTimer _Tick
	' This code will trigger every 10 ms
End Sub

I have read a thread where Erel said:
Timers should be declared as process globals. Otherwise new timers will be created when the activity is recreated.
It is recommended to pause the timers and resume then in Activity_Pause and Activity_Resume.

The question I have is what if I don't pause the timer when moving to another Activity or even when I press the home button and the app is running in the background?

Will that mean my app/Activity will come up with a error next time it loads or even cause the app to run out of memory ?
 

aaronk

Well-Known Member
Licensed User
Longtime User
I only wanted to know what would happen if I didn't do it.
If the worse thing that is going to happen is flatten the battery quicker and not crash the app (such as memory issues) then it's not a really bad thing.

I seem to be getting a out of memory errors for one of my apps, and since it doesn't tell me where this error is in my app I decided to re-write the app (gives me a chance to clean the code up a lot as well) and I come across the part about the timer should be paused etc and just wondering why in case it was the reason for my out of memory issues. The out of memory issue isn't such a big issue at the minute since I am re-writing the app so that's why I haven't reported on the forum.
 
Upvote 0

JiggyMan

Member
Licensed User
Longtime User
...I decided to re-write the app (gives me a chance to clean the code up a lot as well)...
Many years ago I had a developer I admire greatly tell me it's often good to "build one to throw away". The time to worry is when you think you've written the perfect application without having thrown anything away.

Sorry. I don't have anything constructive to add to your thread. Just felt the desire to elaborate on the concept you mentioned. :)
 
Upvote 0
Top