Android Question Correct Commands Order

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Suppose that I want to destroy and recreate a service calling from itself. What is the correct order ?
This:
B4X:
StopService(Me)
StartService(Me)

Or This ?

B4X:
StartService(Me)
StopService(Me)
 

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Both commands send a message to the message queue. I don't see any good reason to destroy and recreate a service. It sounds to me that you are doing something wrong.

The correct order is:
B4X:
StopService(Me)
Sleep(300)
StartService(Me)

However you shouldn't do it.

Thanks @Erel ,
That's because I need to recreate a service in order to restart the Service_Create sub in certain conditions... maybe the follow code should be more elegant:

B4X:
Sub Service_Create
 
     CreateRoutine

end Sub

Sub CreateRoutine

      If condition then
             do something... ' This code will do something that will turn the condition false sometime, stopping the loop...
             ...
             CreateRoutine
     else
            StopService(Me)
     end if
    
end Sub

What do you think @Erel ?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
What do you think @Erel ?
I think that it is a mistake to restart the service. I don't see any good reason for this. If you want to restart something in the service then move the relevant code to a different sub (out of Service_Create) and call it from Service_Create and when you want to restart it.
 
Upvote 0
Top