14
Views
2
Comments
Solved
Mobile app run code in background?
Application Type
Mobile
Service Studio Version
11.10.0 (Build 34711)

Hello,

I'm developing a mobile application and I need to run some code when the app goes to background. I was looking in the outsystems and I only found these two events.

Is there a way to add an event on application pause?


Best regards,

Ricardo

Rank: #95
Solution

Hello Ricardo,

Hope you're doing well.


I had to do a similar thing back in a day for a mobile app that I was working on.

Unfortunately, there is no system event for On Application Pause directly on Service Studio.


However, I managed to do it using some JavaScript code.

Since I wanted to run my logic in every single screen, I created a Block for this purpose that I placed inside my application's layout. This way I was sure that it was present in all of my screens.


In this Block's OnReady event, you just need to add an event listener, like this:

document.addEventListener("pause", $actions.OnPause, false);


Then you just need to create a screen action called OnPause where you can put the logic that you want to run when your app goes to background.


In the same block, I advise you to remove the event listener as well so you don't add several unnecessary events. You may do it using some JavaScript on OnDestroy event:

document.removeEventListener("pause", $actions.OnPause, false);


Alternatively , there is a Forge component that seems to handle some mobile native events, including Pause event:

https://www.outsystems.com/forge/component-overview/3408/mobile-events

I never used it, but I was checking its code and it seems very similar to what I just described. Honestly, I would give it a try :)


Hope that this helps you!


Kind regards,

Rui Barradas

Rank: #54771

Thanks Rui for the fast and type of answer that I was looking for. It solved my problem and now I can do the correct logic in my app. Thanks again!


Best Regards,
Ricardo