Integration in a native Android app
This guide describes how to setup an Android application that integrates with Seamly Web UI using a WebView and how to retain data across initialisations.
This guide assumes that there is a hosted
app.html which uses the built-in
appStorageProvider. Usually this will be provided in your implementation. For this demo we use https://developers.seamly.ai/clients/web-ui/demos/app/index.html.
A demo app for this example can be found at https://gitlab.com/seamly-app/examples/android-demo-app.
Connecting the web page
To integrate Seamly Web UI we need to load the app page which hosts the Seamly Web UI into a
WebView. This should be done in the
onViewCreated (or similar) of the
View that hosts the
WebView. Note that when developing on your own machine, you can't use
localhost and need to use your machine's IP. If you are testing on an emulator on the same machine, however, you can use the address
String url = "https://developers.seamly.ai/clients/web-ui/demos/app/index.html"; WebView webView = (WebView) view.findViewById(R.id.webview); webView.loadUrl(url);
As mentioned, we wil use a
WebView. We should also register the
WebView for the interface. If you want to use your own object substitute it for
this in the code below.
In this example, we will be using Android's
sharedPreferences to store data. The client will never store large amounts of data, so this might be fine for your needs too. If you have another data storage solution in place you can substitute this in the code below. Also verify if the solution matches the sensitivity of the data and your privacy settings.
On the app side, things are relatively simple. We have to declare a method that takes the data and stores it in our storage solution. Since we chose
sharedPreferences and this only stores simple objects, we will pass our data in JSON format. This keeps our function simpler too, as it only needs a single argument:
appStorageProvider will call this
You should be able to build and run the app again, but nothing will change. The data will be stored inside your app, but it isn't being retrieved yet. Let's tackle that next.
We will need the stored data on initialisation. The
appStorageProvider needs to retrieve the data directly when it is created. Due to the asynchronous way Android builds up its
WebView it is quite easy to pass our data on.
All we need to do is go back to the
onViewCreated where we initialized our bridge, check if we have some data and write it to the webpage:
isEmpty() check here also makes sure that we won't try to execute
appStorageProvider uses the data left by our initialisation process.
Our app will now build and run and when you start a chat you can leave it and come back and the chat will continue where you left of. Our storage solution is working! But there is one small detail we overlooked...
Avoiding synchronisation issues
We only transmit the stored data on initialisation. This should be fine, since normally the
setData and add