Fork us on GitHub

Background Fetch

Background work refined with downloading
Post Image

Background Fetch

Background fetch allows an app to periodically "fetch" information from the network while the app is in the background. This is scheduled by the native platform, where apps that support background fetch will be started up (in the background), and their performBackgroundFetch method will be invoked.

Since the app will be launched directly to the background, you cannot assume that the start() method was invoked prior to the performBackgroundFetch call

Implementing Background Fetch

Apps that wish to implement background fetch must implement the BackgroundFetch interface in their main class.

The main class is the one mentioned in the preferences not the state machine or some other class!

On iOS, you also need to include fetch in the list of background modes specifically include fetch in the ios.background_modes build hint e.g.:


Or for more than one mode:


In addition to implementing the BackgroundFetch interface, apps must explicitly set the background fetch interval by invoking Display.getInstance().setPreferredBackgroundFetchInterval(interval) at some point, usually in the start() or init() method.

Platform Support

Currently background fetch is supported on iOS, Android, and in the Simulator (simulated using timers when the app is paused). You should use the Display.getInstance().isBackgroundFetchSupported() call to check if the current platform supports it.


The following code demonstrates simple usage of the API:

 * A simple demo showing the use of the Background Fetch API.  This demo will load
 * data from the Slashdot RSS feed while it is in the background.
 * To test it out, put the app into the background (or select Pause App in the simulator)
 * and wait 10 seconds.  Then open the app again. You should see that the data is loaded.
public class BackgroundFetchTest implements BackgroundFetch {

    private Form current;
    private Resources theme;
    List<Map> records;

    // Container to hold the list of records.
    Container recordsContainer;

    public void init(Object context) {
        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default

        // Pro only feature, uncomment if you have a pro subscription
        // Log.bindCrashProtection(true);

    public void start() {
        if(current != null){
            // Make sure we update the records as we are coming in from the
            // background.
        Display d = Display.getInstance();

        Form hi = new Form("Background Fetch Demo");
        hi.setLayout(new BoxLayout(BoxLayout.Y_AXIS));

        Label supported = new Label();
        if (d.isBackgroundFetchSupported()){
            // This call is necessary to initialize background fetch

            supported.setText("Background Fetch IS Supported");
        } else {
            supported.setText("Background Fetch is NOT Supported");

        hi.addComponent(new Label("Records:"));
        recordsContainer = new Container(new BoxLayout(BoxLayout.Y_AXIS));


     * Update the UI with the records that are currently loaded.
    private void updateRecords() {
        if (records != null) {
            for (Map m : records) {
                recordsContainer.addComponent(new SpanLabel((String)m.get("title")));
        } else {
            recordsContainer.addComponent(new SpanLabel("Put the app in the background, wait 10 seconds, then open it again.  The app should background fetch some data from the Slashdot RSS feed and show it here."));
        if (Display.getInstance().getCurrent() != null) {

    public void stop() {
        current = Display.getInstance().getCurrent();
        if(current instanceof Dialog) {
            current = Display.getInstance().getCurrent();

    public void destroy() {

     * This method will be called in the background by the platform.  It will
     * load the RSS feed.  Note:  This only runs when the app is in the background.
     * @param deadline
     * @param onComplete
    public void performBackgroundFetch(long deadline, Callback<Boolean> onComplete) {
        RSSService rss = new RSSService("");
        records = rss.getResults();

Share this Post:

Posted by Steve Hannah

Steve writes software for Codename One. He is an open source enthusiast who loves to tinker with new technologies.