Android Question Problem with timer

Abbas818

Member
Hi
My B4A app has two activities, one of which has a timer, which executes its subroutine every 1 sec. The problem is that when I am in the other activity, the timer does not stop, and every second the program returns to the timer activity and executes its subroutine.
Can everyone know the solution?
 

DonManfred

Expert
Licensed User
Longtime User
- Switch to B4XPages

or

- Stop the time when you reach Activity_Pause. Start the timer in Activity_Resume

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
    timer1.Initialize("Timer",1000)
End Sub
Sub Activity_Pause (UserClosed As Boolean)
    timer1.Enabled = False
End Sub
Sub Activity_Resume
    timer1.Enabled = True
End Sub
Sub Timer_Tick
    Log($"Timer-Tick"$)
End Sub

When you switch to second activity the timer ill be stopped. When you come back to actitiy 1 it will be restartet...

See https://www.b4x.com/android/forum/threads/android-process-and-activities-life-cycle.6487/
 
Upvote 1

Abbas818

Member
- Switch to B4XPages

or

- Stop the time when you reach Activity_Pause. Start the timer in Activity_Resume

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
    timer1.Initialize("Timer",1000)
End Sub
Sub Activity_Pause (UserClosed As Boolean)
    timer1.Enabled = False
End Sub
Sub Activity_Resume
    timer1.Enabled = True
End Sub
Sub Timer_Tick
    Log($"Timer-Tick"$)
End Sub

When you switch to second activity the timer ill be stopped. When you come back to actitiy 1 it will be restartet...

See https://www.b4x.com/android/forum/threads/android-process-and-activities-life-cycle.6487/
I did this method but the problem remains.
 
Upvote 0

Abbas818

Member
I strongly recommend to switch to B4XPages. Things will simply work as you expect and you will not need to deal with the many challenges related to the complex activities life cycle.
I have a simple program, only If I uses Okhttputils2 to get data from google sheet in Sub Timer1_Tick this problem occurs.
 
Upvote 0
Top