Android SDK best practice
Mobile application performance is critical to your business. Our technology is focused on improving your customer's experience with your app. As such, screen performance is a core component of our solutions. To boost engagement and conversion rates, we employ several approaches to minimize screen load times.
Jetpack Compose Component
Starting from major version 4 of the Rokt Android SDK, you can add a Rokt layout by utilizing the RoktLayout
composable. This removes the need to call Rokt.execute
and supports a more modern declarative integration using Compose as outlined in Add a placement.
Adding the component
import com.rokt.roktsdk.RoktLayout
@Composable
fun MainScreen(modifier: Modifier = Modifier) {
Column(
modifier = modifier
.background(Color.LightGray)
.padding(8.dp),
) {
// if application supports only light mode
val config = remember {
RoktConfig.Builder().colorMode(RoktConfig.ColorMode.LIGHT).build()
}
RoktLayout(
sdkTriggered = true,
viewName = "RoktExperience",
attributes = mapOf(
"email" to "j.smith@example.com",
"firstname" to "Jenny",
"lastname" to "Smith",
"mobile" to "(323) 867-5309",
"postcode" to "90210",
"country" to "US"
),
location = "RoktEmbedded1", // If using an embedded layout
onLoad = {},
onUnload = {},
config = config,
onShouldShowLoadingIndicator = {},
onShouldHideLoadingIndicator = {},
)
}
}
You can use the RoktLayout
composable for both embedded and overlay layouts.
Using App Configurations
Applications can now pass the configuration settings from their own application environment. This allows Rokt SDK to use application's custom configurations instead of relying solely on system defaults.
ColorMode object
Value | Description |
---|---|
LIGHT | Application is in Light Mode |
DARK | Application is in Dark Mode |
SYSTEM | Application defaults to System Color Mode |
import com.rokt.roktsdk.Rokt
// if application supports only Light Mode.
val roktConfig = RoktConfig.Builder().colorMode(RoktConfig.ColorMode.LIGHT).build()
Rokt.execute(
// other params,
config = roktConfig,
)
CacheConfig object
Parameter | Description |
---|---|
cacheDuration | Optional duration in seconds for which the Rokt SDK should cache the experience. Maximum allowed value is 90 minutes and the default (if value is not provided or invalid) is 90 minutes. |
cacheAttributes | Optional attributes to be used as cache key. If null, all the attributes sent in Rokt.execute will be used as the cache key. |
import com.rokt.roktsdk.Rokt
// to cache the experience for 1200 seconds, using email and orderNumber attributes as the cache key.
val roktConfig = RoktConfig.Builder().cacheConfig(
CacheConfig(
cacheDurationInSeconds = 1200,
cacheAttributes = mapOf("email" to "j.smith@example.com", "orderNumber" to "123")
)
).build()
Rokt.execute(
// other params,
config = roktConfig,
)
Using Font Typefaces
If you are using your own font typefaces as outlined in Initialize the Android SDK, you need to pass a map of weak references to your typeface objects in execute
.
- Java
- Kotlin
import com.rokt.roktsdk.Rokt
class ConfirmationActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
...
// Include any appropriate consumer attributes
val attributes = mapOf(
"email" to "j.smith@example.com",
"firstname" to "Jenny",
"lastname" to "Smith",
"mobile" to "(323) 867-5309",
"postcode" to "90210",
"country" to "US",
)
val fontTypefaces = mapOf(
"Arial-Bold" to WeakReference(yourTypefaceObject),
)
Rokt.execute("RoktExperience",
attributes,
object : Rokt.RoktCallback {
override fun onUnload(reason: Rokt.UnloadReasons) {
}
override fun onLoad() {
}
override fun onShouldHideLoadingIndicator() {
}
override fun onShouldShowLoadingIndicator() {
}
},
fontTypefaces,
)
...
}
}
import com.rokt.roktsdk.Rokt;
class ConfirmationActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
...
Map<String,String> attributes = new HashMap<String, String>();
attributes.put("email", "j.smith@example.com");
attributes.put("firstname", "Jenny");
attributes.put("lastname", "Smith");
attributes.put("mobile", "(323) 867-5309");
attributes.put("postcode", "90210");
attributes.put("country", "US");
Map<String, WeakReference<Typeface>> fontTypefaces = new HashMap<>();
fontTypefaces.put("Arial-Bold", new WeakReference<>(yourTypefaceObject));
Rokt.INSTANCE.execute("RoktExperience",
attributes,
new Rokt.RoktCallback() {
@Override
public void onLoad() {
}
@Override
public void onUnload(Rokt.UnloadReasons unloadReasons) {
}
@Override
public void onShouldHideLoadingIndicator() {
}
@Override
public void onShouldShowLoadingIndicator() {
}
},
fontTypefaces
)
...
}
}
For more information see how-to section.