Can't stop For Loop and reload activity

nubi

Member
Licensed User
Longtime User
Hello everybody,

I'm trying to make a simple countdown timer in which on the first layout you set the time in seconds you want it to count backwards and click on a button to set it and open the next Activity. (So far so good)

On the new layout of the second activity and a new layout you have two buttons (1) Start and (2) Stop. The idea is that when you press "Start" a progress bar is shown in a backwards fashion and a sound will be played (not implemented yet). If the "Stop" button is pressed the countdown should stop reinitialize and wait for the "Start" button to be pressed again.

The problem I'm having is once the countdown starts after pressing "Start" the countdown does not stop until it has completed the for loop. And once it finishes neither of the buttons do anything.:BangHead:

Here's my code:

B4X:
Sub btnStart_Click

For I=Main.T To 1 Step -1
PBar.Progress=(C*I) ' Math so Progress Bar looks good
Sleep(1000) ' Pause time for Progress Bar countdown
Next

End Sub

B4X:
Sub btnStop_Click

I=Main.T

End Sub

** I don't have any code in the subs Activity_Resume or Pause, don't know if that will affect anything.


Thanks in advance.
 

stevel05

Expert
Licensed User
Longtime User
You should look at using a timer to drive the count down, it's then a simple case of disabling the timer and resetting the count when stop is clicked.
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
Better use a timer to do something like that, take a look at the attached sample, it is a very basic sample of what you are trying to achieve.

I hope it will give you an idea.
 

Attachments

  • TimerSample.zip
    7 KB · Views: 275
Upvote 0

nubi

Member
Licensed User
Longtime User
SOLVED.

Thank you for your suggestions and code sample. It is definitely much easier to control with the Timer.

:sign0188:
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…