Skip to Content

Offline-Enable Your Android Application

Enable offline OData in your Android application, resulting in an application that can be used without a network connection and that performs data requests with less latency.
You will learn
  • How the offline feature works, through demonstration
  • How the synchronization code works
  • How to handle errors that occur while syncing

flyingfish162Bruce MengJanuary 24, 2023
Created by
sandeep-tds
January 9, 2023
Contributors
sandeep-tds
  • Step 1
    1. Follow the instructions at Try Out the SAP BTP SDK Wizard for Android to create a new application using the SAP BTP SDK Wizard for Android and select Offline for the OData option on the Project Features tab. The push feature is not needed for this application.

      Choose Offline OData
    2. Run the app. After the login process, a screen is displayed explaining that the offline store is opening. As the screen suggests, opening the offline store for the first time can take up to a few minutes. One technique to decrease this initial time is to only download data that is relevant to the user, such as customers that belong in their sales region.

      Offline store opening
    3. When you get to the app’s home page, turn on airplane mode on your device, or disable Wi-Fi and data.

      Turn on Airplane mode
    4. The entity list screen is populated based on the metadata.xml file retrieved when the application was created. Tap the Products list item.

      Entity list screen

      The Products screen makes a data request to display the available products. Notice that it succeeds without a working network connection. The data request is fulfilled from the offline store that was previously created and populated on the device. Tap the Accessories item to display the detail screen.

      Select the second product
    5. On the detail screen, tap the edit toolbar icon.

      Select product edit button
    6. Make a change to the currency code and tap the save toolbar icon.

      Change currency code
    7. Navigate back to the app’s Home screen and tap Synchronize using the three-dot-menu in the top right of the title bar.

      Attempt a sync

      The sync should fail because you haven’t turned airplane mode off yet.

      Sync fails
    8. Turn off airplane mode or re-enable Wi-Fi/data and attempt a sync again. You will see a notification that describes the sync action.

      Syncing notification

      When the sync completes, the change you made will have been applied to the back end.

    Which of the following is true the first time an offline-enabled app is opened?

  • Step 2

    Make sure you are selecting the right language above.

    To protect the data in offline store, you must enable offline store encryption by supplying an encryption key.

    For additional information about multiple user mode, see Enable Multi-User Mode for Your Android Application.

  • Step 3

    Defining queries tell the OfflineODataProvider (the class that manages the offline store) which entity sets to store on the device. In the case of the wizard-generated application, there is a defining query for each available entity by default, meaning that each entity is stored offline and available if the user doesn’t have an internet connection. For more information, see Defining Queries.

    With an offline-enabled app, requests made against the entity sets that are included in the defining requests will always be fulfilled from the local offline store.

    What is the purpose of a defining query?

  • Step 4

    The application allows users to make changes against a local offline store and synchronize manually at any time. The sync operation is performed by a worker. There are three operations that must be implemented in order to use the offline store functionality: open, download, and upload. As their names suggest, the operations open the offline store, download server changes, and upload user changes, respectively. In the wizard-generated application, OfflineOpenWorker implements open during initialization and OfflineSyncWorker implements a sync operation, which requires download first, and then upload.

    For more information about how the offline store works, see the Working With Offline Stores.

    What does the method synchronize do?

  • Step 5

    When syncing changes made while offline, conflicts can occur. One example might be if two people attempted to update a description field for the same product. Another might be updating a record that was deleted by another user. The ErrorArchive provides a way to see details of any of the conflicts that may have occurred. The following instructions demonstrate how to use ErrorArchive.

    1. Update a SalesOrderItem and change its quantity to be zero and save it. Update a second item and change its quantity to a different non-zero number and save it.

      Create SalesOrderItem Button
      Edit SalesOrderItem Button
      Create with zero quantity

      Notice that the items are now marked with a yellow indicator to indicate that an item has been locally modified but not yet synced.

      Modified but not yet synced
    2. Attempt a sync, and you’ll notice that the sync completes, but if you examine the SalesOrderItems list, one item has a red mark beside it, indicating it is in an error state. This is because the back end has a check that SalesOrderItems cannot have zero for their quantity. This check does not exist in the local offline store, so the update succeeds locally but fails when the offline store is synced.

      Sync Error
  • Step 6

    In this section we will create an Error Information screen that displays the details from the ErrorArchive.

    Further information on using the offline feature can be found at Step by Step with the SAP BTP SDK for Android — Part 6 — Offline OData.

    Congratulations! You have created an offline-enabled app using the SAP BTP SDK Wizard for Android and examined how the ErrorArchive can be used to view synchronization errors!

Back to top