Android Question Timer seems to restart itself without being told to do so

CoderMaan

Member
Licensed User
Longtime User
Hello,

I wonder if you can help me with a timer problem? The attached program sometimes works as intended on a real device, but I'd say it does so less than 50% of the time (in either debug or release mode), and it almost always fails to run correctly when I remove the Do While loop (that was inserted for troubleshooting purposes only).

The feature I am having trouble with, i.e., the operation of the "Auto Forward" button (on the only included layout) is supposed to perform as follows:

1) When the app starts, a countdown timer is displayed on the AutoFwd button, and it shows a countdown in action (i.e., 10 ... 9 ... 8 ..., with a new number displaying every second). This feature seems to be working as intended; a future version will include code to start another activity when the countdown reaches 0, but that is irrelevant at this point.

2) When the user holds this button down for MORE than (approximately) 2 seconds, the countdown restarts at 10. This feature also seems to be working as intended.

3) If the user presses and releases the button more quickly (i.e., holds it for LESS than 2 seconds) WHILE THE COUNTDOWN IS IN PROGRESS, "...Timer1" is supposed to stop altogether, with the effect of the countdown stopping altogether (since the number being displayed can only change when Timer1 is actually running). Well, sometimes it does stop and sometimes it does not!

4) If the user presses and releases the button quickly (i.e., holds it for LESS than 2 seconds) WHILE THE COUNTDOWN IS NOT IN PROGRESS (i.e., it is suspended), Timer1 is supposed to start up again and the countdown is supposed to continue (from where it originally stopped). Well, this operation works once in a while, but certainly not all of the time.

My main question has to do with the fact that the code (part of an If-Then statement) that ENABLES Timer1 (very shortly after it had been DISABLED), seems to be getting executed without any action by the user. In particular, although the program seems to correctly detect each button UP event, it also seems to be detecting some "button UP" events that did not actually occur; I say this because I have verified (or at least I think I have) that it is the (...Timer1.enabled = true) code in one of my If-Then statements that is causing Timer1 to restart, even though the user simply pressed the button quickly and let go (one time!) ... and this code should not be running. FYI, Timer2 starts on a button DOWN event, and it stops on a button UP event; and it seems to be working correctly, at that.

To repeat something said above, the (only) Do-While loop is my program was inserted for trouble-shooting purposes only. If the features (3 & 4) described above do not work for you at all, then perhaps they will work for you if you adjust the number in the Do-While loop (to 10000 or 30 or 40000, say?). This loop does essentially nothing except to keep some other lines of code from executing (for a very brief time!), but the question is, why should that loop---or the MsgBox that has been commented out---make any difference whatsoever in whether or not Timer1 actually stops and remains stopped (after a short click)?

As a final point of clarity, I am using the button DOWN and UP events, instead of the CLICK and LONG CLICK events, so that I have better control over the timing of a long click. However, now that I've spent so much time (many, many hours) trying to get this feature to work, it is also a matter of principle; i.e., I'd really like to use the down and up events for this feature, and all that I really want is some (very robust) code to do the job. If you see a definite fix for my code, please let me know. Alternatively, if you can provide me with some altogether different code that definitely does the job, that would be fine too!

Thanks for any words or wisdom, and especially for the code or code corrections that you can send my way!

CoderMaan,
July 15, 2014
 

Attachments

  • B4A_Program_w_TimerProblem.zip
    9 KB · Views: 186

NJDude

Expert
Licensed User
Longtime User
Well, if you are stopping the timer when the user taps on a button means that the timer will not stop if the user fails to do it, or, if the user taps on the BACK key or HOME key, placing the Timer1.Enabled = False on Activity_Pause will stop it for sure, you can still use a button of course.
 
Upvote 0

CoderMaan

Member
Licensed User
Longtime User
Sorry, but I am still confused (perhaps that's because I'm a complete novice at object-oriented programming!). I think you are saying that, in order to handle some peripheral things that might happen when my program is running (e.g., the user presses the BACK or HOME key), the only completely safe thing to do is to insert some code in the Activity_Pause sub that will stop the timer(s). If that's what you are saying then I must agree that I should add that code "to be safe." However, my point is that the program is not working as intended even in the event that nothing is happening "in the periphery." So, I really don't see the connection between that (needed) code in Activity_Pause and the fact that my program is not working as intended, when the ONLY user action is a quick press of the "AutoFwd" button. After all, I surely need to use the button UP event to know that the button has been released ... so what does that have to do with the Activity_Pause routine?

I wonder if you are also suggesting that (the currently empty) Activity_Pause routine is, be default, "running" when there is no other action by the user ... after a button UP event. But that would not seem right, since the Timer1_Tick's are still occurring (if Timer1 is enabled, that is).

If you or someone could speak about how the two of these routines (button UP and Activity_Pause) should be made to interact, perhaps that would help. Or, as stated in the original question, some actual code with the desired effect might help. Thanks for the quick replies, nonetheless! I'll check back tomorrow for more, I hope...
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You are initializing the timers multiple times. You need to only initialize the timers when FirstTime is true.

Also remove this Do While loop. It will only cause problems. And I also recommend you to remove the ExitApplication call. This is not the standard way to close an app in Android.
 
Upvote 0
Top