Background/foreground modes

Describes how to handle background or foreground modes in mobile apps.

Mobile apps foreground and background modes

Mobile apps can change their state from background mode to foreground mode and vice-verse. For example, when the user switches to another app, or when going back to the home screen, the current app goes to background mode. An app goes to foreground mode, when it is re-selected from the active apps list.

Detecting foreground/background mode changes

Genero BDL provides two predefined actions, to detect when the state of a mobile app changes:
  1. The action enterbackground is fired, when the app goes to background mode.
  2. The action enterforeground is fired, when the app goes to foreground mode.
Note: The enterforeground action is not fired when the app starts. This action is only fired when returning to foreground mode, after it was in background mode.
To execute code when the app goes to background or foreground mode, use on ON ACTION handler:
   ON ACTION enterbackground
      LET skip_timers = TRUE
   ON ACTION enterforeground
      IF NOT ask_password() THEN
         EXIT PROGRAM
      ENF IF

For example, when the app enters background mode, it is recommended that the program suspend any activity, and skip code that would be executed in ON TIMER triggers.

On the other hand, when the app enters foreground mode, the program can for example ask the user's login/password again, for security reasons, to make sure that the mobile device did not end up in other hands while the app was in background mode.

To control action view rendering defaults and current field validation behavior when the enterforeground / enterbackground actions are used, consider setting action default attributes for these action in your .4ad file as follows:
<ActionDefaultList>
  ...
  <ActionDefault name="enterbackground" validate="no" defaultView="no" contextMenu="no"/>
  <ActionDefault name="enterforeground" validate="no" defaultView="no" contextMenu="no"/>
  ...
</ActionDefaultList>
Another option is to define these action defaults attributes in the ON ACTION handlers:
ON ACTION enterbackground (VALIDATE=NO, DEFAULTVIEW=NO)
   ...
ON ACTION enterforeground (VALIDATE=NO, DEFAULTVIEW=NO)
   ...

See also List of predefined actions.

Checking if the app is currently in foreground mode

To know the current state of a mobile app, use the mobile.isForeground front call:
DEFINE fg BOOLEAN
CALL ui.Interface.frontCall("mobile", "isForeground", []. [fg] )
IF fg THEN
   ...
END IF

What does the app when in background mode?

On iOS, when the app is in background mode, the program cannot do anything meaningful outside push notifications or audio play.

On Androidâ„¢, when the app is in background mode, the program can continue its execution.

Avoid Android to terminate app when in background (GMA)

On Android devices, an app can switch between foreground to background states.

The Android system can decide to stop an app in background state, for example when resources are required for other apps.

Genero programs running on servers are typically not prepared to be stopped at any time; except in case of major failure, it's the program that decides when it terminates. On mobile devices, Android can decide to stop the app when it is in background state.

By default, when the app goes to background state, a notification is shown by GMA, to keep the app in foreground state, and avoid Android stopping the app. The notification disappears, when the app returns to foreground state.

Use the androidKeepForeground style attribute to control the way the GMA forces Android to keep your app alive. Set this attribute to "no" if your app can be stopped by Android, when it is in background state. When this style attribute is set to "no", GMA will not display a notification, when the app switches to background mode.

Important: When using androidKeepForeground=no, Android may stop the app at any time. Make sure that the code is ready for this case.

For more details, see UserInterface style attributes.