Timer Still Enabled After Activity.Finish???

Bill Norris

Active Member
Licensed User
Longtime User
I've made a puzzling discovery. I've been having an issue with a timer control acting erratically. More specifically, the timers purpose to to alternate between two colors for a button at half-second intervals. I began to notice that the timing would occasionally be erratic -- from stopping altogether to flashing faster and not evenly. So, I put a log message in the timer code to help me see what is going on - I write to the log with each tick. The puzzling discovery was when I exited the activity with activity.finish, the timer log messages continued to post. I am wondering why a timer would continue to fire even after activity.finish. I solved the problem with timer.enabled=false in the activity ending code, but this seems strange to me.
 

margret

Well-Known Member
Licensed User
Longtime User
This is not really strange. The Activity functions that pass to the core OS is still running as before after the Activity.Finish. This just returns to the calling activity and tells the OS it's OK to kill this activity when the OS needs/wants to.

If you start the MediaPlayer and issue the play command and then Activity.Finish. You will see it too keeps playing.

So Timer.Enabled = False is the right thing to do.

Did you log in the Tick event like: Log(DateTime.Now /1000), to see if the interval in the tick is the same?

May be that the views are not updating like they should even if the timer interval is the same.
 
Last edited:
Upvote 0

Bill Norris

Active Member
Licensed User
Longtime User
RE:

The big surprise here is that when I added the enabled=false to kill the timer on Finish, the erratic timing issues go away. When I take out the enabled=false statement, the problem returns. At least I seem to have fixed the problem, only thing is by all accounts, it doesn't appear as though there should have been a problem in the first place.
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
Now I know what you are talking about. I have seen and had the same issue. As I said before, when you left the activity the timer was still running. Now when you reloaded the app it set another timer to the same tick event that might of been a quarter second behind the first. Each time you would restart the app another timer was set. Now there are three timers calling tick event. This is not an issue with b4a but the OS. One of mine did the same on Android 2.2 and 2.3 but on 3,0 and 3.1 no issues. Glad you found it.
 
Upvote 0

Bill Norris

Active Member
Licensed User
Longtime User
RE:

I suspected something like that might be happening -- multiple timer instances conflicting with each other. Glad to know I wasn't losing my mind.
 
Upvote 0
Top