Skip to content
Android example app to understand how to use data persistence with Sync API
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Build an example app using Sync API and Persistence Library with Contentstack’s Android SDK

This is an example app built using Contentstack’s Android SDK, Sync API, and Persistence Library. You can try out and play with our Sync API and data persistence with this example app, before building bigger and better applications.

The Persistence Library lets you store data on the device’s local storage, helping you create apps that can work offline too. Perform the steps given below to use the app.

Perform the steps given below to use this app.


In this tutorial, we will first go through the steps involved in configuring Contentstack and then look at the steps required to customize and use the presentation layer.

Step 1: Create a stack

Log in to your Contentstack account, and create a new stack. Read more about stack.

Step 2: Add a publishing environment

Add a publishing environment to publish your content in Contentstack. Provide the necessary details as per your requirement. Read more about environments.

Step 3: Import content types

For this app, we need just one content type: Session. Here’s what it’s needed for:

  • Session: Lets you add the session content to your app

For quick integration, we have already created the content type. Download the content type and import it to your stack. (If needed, you can create your own content types. Read more about Content Types).

Now that all the content types are ready, let’s add some content for your Stack.

Step 4: Adding content

Create and publish entries for the ‘Session’ content type.

Now that we have created the sample data, it’s time to use and configure the presentation layer.

Step 5: Clone and configure the application

To get your app up and running quickly, we have created a sample app. Clone the Github repo given below and change the configuration as per your need:

$ git clone

Now add your Contentstack API Key, Delivery Token, and Environment to the project during the SDK initialization step. (Find your Stack's API Key and Delivery Token.)

Stack stack = Contentstack.stack(context,“api_key”, “delivery_token”, “environment");

This will initiate your project.

Step 6: Install and set up Realm

We need to first download Realm and install it. To do so, perform the steps given below:

  • Add the latest version of Realm library in your project and follow Installation gulde to complete setup.

Step 7: Install Contentstack Android SDK and SyncManager

Now that your Realm installation is ready, let's look at the steps involved in setting up your Contentstack SDK.

  1. Download and set up the Contentstack android SDK. Read the Contentstack android SDK Documentation for more details.

  2. You will find the "syncwrapper" folder, which contains the following four files keep it in your src folder:


Step 8: Map data

There are three important items to be mapped in our Synchronization process:

  • Sync token/pagination token
  • Entries
  • Assets

Let’s look at how to persist Sync Token & Pagination Token

Sync token/pagination token

To save Sync Token and Pagination Token, we need SyncStore class file which will manage the storage and retrieval of updated Sync Token and Pagination Token.

if (stackResponse.getPaginationToken()!=null){

Entry Mapping

To begin with, let’s consider an example of our Example app. Let’s say we have content type: Session. Let’s see how to implement this example.

Create a table class named Session extending RealmObject, and add following code to implement EntityFields as shown below:

// @RealmClass accepts ("name= "content_type_uid"")
@RealmClass(name = "session")
public class Session extends RealmObject {

   // Mandatory fields
   @RealmField(name = "uid")
   private String uid;

//Note: the annotation name will be content_type's field id
//user defined fields may be anything of user liking.
   private String mTitle;

   private String mIsPopular;

   private String mType;

   private String mId;

   private String mTags;

   private String mLocale;

   private RealmList<Speaker> speaker;

   private String mTrack;

   private String mStartTime;

   private String mEndTime;

   private Room mRoom;

  ... Generated getters and setters ...


You also need to implement the fieldMapping function which returns the mapping of attributes and entry fields in Contentstack.

Similarly, we can add other entries and mapping for each entry.

Asset Map

To map Assets, you need to create a table for assets named SysAssets and extend RealmObject. Add the following code to implement AssetProtocol.

public class SysAssets extends RealmObject {

   // Mandatory fields
   @RealmField(name = "uid")
   private String uid;

 // Note: the annotation name will be content_type's field id
 //user defined fields may be anything of user liking.
   private String created_at;

   @RealmField(name = "updated_at")
   private String updated_at;
   @RealmField(name = "created_by")
   private String created_by;
   @RealmField(name = "updated_by")
   private String updated_by;
   @RealmField(name = "content_type")
   private String content_type;
   @RealmField(name = "file_size")
   private String file_size;
   @RealmField(name = "tags")
   private String tags;
   @RealmField(name = "filename")
   private String filename;
   @RealmField(name = "url")
   private String url;
   @RealmField(name = "is_dir")
   private String is_dir;
   @RealmField(name = "parent_uid")
   private String parent_uid;
   @RealmField(name = "_version")
   private String version;
   @RealmField(name = "title")
   private String title;
   @RealmField(name = "publish_details")
   private String publish_details;

   ... Generated getters and setters ...


Now, our final step is to initiate SyncManager and begin with the Sync content type.

Step 9: Initiate SyncManager and Sync

Finally, after setting up the content mapping, initiate SyncManager. It takes Stack instance and RealmPersistenceHelper class instance as follows:

//Get stack instance like below
Stack stack = Contentstack.stack(context, "api_key", "access_token", "environment");

//Get realm instance like below
   Realm realmInstance = Realm.getDefaultInstance();
//Get realmStore instance by passing realmInsatance to its constructor like below.
   RealmStore realmStore = new RealmStore(realmInstance);
   SyncManager syncManager = new SyncManager(realmStore, stack);


More Resources

You can’t perform that action at this time.