I am trying to make my program use less resources when I send it to the background through overriding the onClose() function. My first step is to stop it painting text and gauge fields.
I’ve been reading this doc on Efficiency,
“Eliminating unnecessary processing on the device”
“You can use methods to stop animating or repainting the screen when the screen is not visible, and resume when the screen is visible again. You can override Screen.onExposed(), which is invoked when your application’s screen is on top of the display stack and displayed to the user. You can override Screen.onObscured(), which is invoked when your application’s screen is not displayed to the user or is obscured by another screen.”
I know if I use isForeground(), it will stop it from painting if my app is in the background, but will it do the same if it is obscured by another app? It seems much more simpler to use isForeground()
private boolean isExposed;
protected void onExposed()
{
isExposed = true;
}
protected void onObscured()
{
isExposed = false;
}
public void batteryStatusChange(int status)
{
// TODO Auto-generated method stub
if ((status & DeviceInfo.BSTAT_LEVEL_CHANGED) != 0)
{
//is there a difference between the two if's?
//if(isExposed)
//if(UiApplication.getUiApplication().isForeground())
{
batteryStatusField.setText(getBatteryLevel());
bitGauge6.setValue(DeviceInfo.getBatteryLevel());
}
}
}
public boolean onClose()
{
UiApplication.getUiApplication().requestBackground();
return true;
}
In general,
isForeground()tells you if your app is running in the foreground. However, as you’ve implemented it, theisExposedvariable only tells you when the screen that it belongs to has been exposed, or obscured (because you’re setting it in theScreen.onExposed()andScreen.onObscured()methods).For your app, maybe you only have one
Screensubclass?Most apps will have many screens. So, in that situation, the
isExposedvariable would only be telling you whether or not one (of many) screens is showing.If your app only has the single
Screen, then either technique should work for you.However, as I said in my comment, if the code you’ve posted is showing all that you’re doing in the
batteryStatusChange()callback, then I don’t think you really need to worry about performance. It’s good to be considerate of performance on mobile devices, but neither of the UI calls you make in that method should incur any significant performance cost.