Android Question Generally stop services when the activitiy is paused?

KMatle

Expert
Licensed User
Longtime User
I always stop services I've started via code (like a communication service) when the activity is paused and start them new if I need them (e.g. in resume). So far I never had problems doing this and I find it a good practice to "close" all unneeded ressources.

Any other thoughts about it?
 

MarkusR

Well-Known Member
Licensed User
Longtime User
doing things in the activity pause or resume event make always any trouble in the app flow, i try to avoid.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
@markus
About which troubles exactly do you talk ?

the last one was a endless loop because the runtime permission put the activity in pause because a os dialog appear :)
and after deny and back resume to the app the request starts again. then i found a better solution to handle it.
or if you use back in navigation or finish other activity the resume do something unwanted.
or if you open other activity the pause do something unwanted.
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
With runtime permissions all is clear. Like with activating another apps (for example, Camera) using intent.
But your app should expect resume - pause anyway, because user is able, for example, to click "Home" or "OverView" button.

Which troubles / side effects from OS do you know ?
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
With runtime permissions all is clear. Like with activating another apps (for example, Camera) using intent.
But your app should expect resume - pause anyway, because user is able, for example, to click "Home" or "OverView" button.
Which troubles / side effects from OS do you know ?

theoretical yes everything seems fine until you test it ;) especially if u used copy/paste before.
not know any troubles / side effects from OS.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I always stop services I've started via code (like a communication service) when the activity is paused and start them new if I need them (e.g. in resume). So far I never had problems doing this and I find it a good practice to "close" all unneeded ressources.

Any other thoughts about it?
It really depends on your requirements. There is no right or wrong here.

One of the main advantages of using services is that you are not tied to the activities life cycle. Nothing bad happens if the service keeps running in the background for a few minutes.
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
Nothing bad happens if the service keeps running in the background for a few minutes.

We talk about a style of programming. When everybody thinks - nothing bad that my app will eat a little resources without need, modern computers/smartphones are very powerful, the result will be limitations like in Oreo 8.
 
Upvote 0
Top