Since Symbian OS is a multitasking operating
system, the Application Management Software (AMS) can move your MIDlet
to the background to allow another application to execute concurrently.
After a while, your MIDlet may then be sent back to the foreground.
In S60 5th Edition and 3rd Edition FP2 devices, the
MIDlet lifecycle methods are not called by default. To request the AMS
to invoke MIDlet.startApp() and MIDlet.pauseApp(), you need to add the Nokia-MIDlet-Background-Event proprietary JAD attribute with the value "pause". By default, the attribute value is "run" and pauseApp() and startApp() are not called by the AMS. An alternative way to deal with the MIDlet lifecycle methods not being invoked is to override the Canvas.hideNotify() method, which is called when the MIDlet is sent to the background, and the Canvas.showNotify() method, which is called when it is moved to the foreground.
What you should ensure is the handling of state
switching in both cases so that your application behaves similarly on
Symbian smartphones from different vendors. A possible solution would be
to decouple the handling of state switching from the specific
notification events to handle them in a separate location.
public class StateHandler{
public synchronized void handleSwitchToBackground(){
background = true;
// TODO: handle switching to background
}
public synchronized void handleSwitchToForeground(){
background = false;
// TODO: handle switching to foreground
}
}
Then, MIDlet lifecycle methods delegate the calls to the decoupled location:
public void startApp() {
handler.handleSwitchToForeground();
}
public void pauseApp() {
handler.handleSwitchToBackground();
}
Canvas notification methods also delegate the calls to the decoupled location:
protected void hideNotify(){
handler.handleSwitchToBackground();
}
protected void showNotify(){
handler.handleSwitchToForeground();
}
In Canvas-based MIDlets, for high-level UI screens, the MIDlet could periodically check Displayable.isShown() on the current Displayable
object. For example, a separate thread could check every once in a
while (e.g., every second) if the current displayable object is visible.
If it is not, the application is moved to the background state and, if
it is, the application is moved to the foreground state.
Requesting a return to the foreground is possible but there are differences in the implementation. On some platforms, calling MIDlet.resumeRequest() is required, while on others calling Display.setCurrent() with a displayable object is required. Applications can switch themselves to the background on Nokia devices by calling Display.setCurrent(NULL).
public void goToBackground(){
// will work on Nokia devices
Display.getDisplay(this).setCurrent(null);
}
public void returnToForeground(){
// duplicate action to ensure it is performed on various devices
this.resumeRequest();
Display.getDisplay(this).setCurrent(current);
}