Skip to content

Latest commit

 

History

History

sample-app-android-kotlin

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Kontent.ai Sample Spring Android (Kotlin)

The showcase of the Android application written in Kotlin using Kotlin Coroutines for data fetching from Kontent.

Application walkthrough

Get started

First, build the whole monorepo, and then you could install the app from:

  • Debug version - /sample-app-android-kotlin/build/outputs/apk/debug/sample-app-android-kotlin-debug.apk
  • Unsigned release version - /sample-app-android-kotlin/build/outputs/apk/release/sample-app-android-kotlin-release-unsigned.apk

Alternatively, you could run the application on your Android device, alternatively use the Android emulator.

Features

Application is showcasing a simple listing screen with Article content type.

This application demonstrates loading data from Kontent.ai using Java SDK in Kotlin application using Kotlin Coroutines. It is not meant to be used as a boilerplate.

⚠ There are two Android-specific rules you need to follow in order for the Delivery SDK to work correctly. First is to disable template engine integration when instantiating the client and the second is to avoid using scanClasspathForMappings method.

Data loading using Kotlin coroutines

The data from Kontent.ai is fetched using the Kotlin Coroutines. The CompletionStage returned from the Java SDK is using org.jetbrains.kotlinx:kotlinx-coroutines-jdk8 package to integrate with Kotlin coroutines API. The org.jetbrains.kotlinx:kotlinx-coroutines-android package is used to provide a simple API to synchronize the coroutines and Android lifecycle. This allows to easily synchronize IO and UI operations with proper thread. Take a look at ArticlesActivity::onCreate method to see the actual implementation.

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.articles_activity)

    listView = findViewById<ListView>(R.id.articles_list_view);

    GlobalScope.launch(Dispatchers.Main) { // Threads Synchronization
        val articles = withContext(Dispatchers.IO) { loadArticles() }
        displayArticles(articles);
    }
}

@WorkerThread
private suspend fun loadArticles(): MutableList<Article> {
    val client = DeliveryClientProvider.client;

    val params = DeliveryParameterBuilder.params()
        .filterEquals("system.type", "article")
        .build()
    return client.getItems(Article::class.java, params).await();
}