Files
posthog.com/contents/docs/integrate/feature-flags-code/_snippets/feature-flags-code-android.mdx
2024-11-20 14:11:04 +01:00

66 lines
1.8 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

### Boolean feature flags
```kotlin
import com.posthog.PostHog
if (PostHog.isFeatureEnabled("flag-key")) {
// Do something differently for this user
// Optional: fetch the payload
val matchedFlagPayload = PostHog.getFeatureFlagPayload("flag-key")
}
```
### Multivariate feature flags
```kotlin
import com.posthog.PostHog
if (PostHog.getFeatureFlag("flag-key") == "variant-key") { // replace 'variant-key' with the key of your variant
// Do something differently for this user
// Optional: fetch the payload
val matchedFlagPayload = PostHog.getFeatureFlagPayload("flag-key")
}
```
### Ensuring flags are loaded before usage
Every time a user opens the app, we send a request in the background to fetch the feature flags that apply to that user. We store those flags in the storage.
This means that for most screens, the feature flags are available immediately **except for the first time a user visits**.
To handle this, you can use the `onFeatureFlags` callback to wait for the feature flag request to finish:
```kotlin
import com.posthog.PostHog
import com.posthog.android.PostHogAndroidConfig
import com.posthog.PostHogOnFeatureFlags
// During SDK initialization
val config = PostHogAndroidConfig("<ph_project_api_key>").apply {
onFeatureFlags = PostHogOnFeatureFlags {
if (PostHog.isFeatureEnabled("flag-key")) {
// do something
}
}
}
// And/Or manually the SDK is initialized
PostHog.reloadFeatureFlags {
if (PostHog.isFeatureEnabled("flag-key")) {
// do something
}
}
```
### Reloading feature flags
Feature flag values are cached. If something has changed with your user and you'd like to refetch their flag values, call:
```kotlin
import com.posthog.PostHog
PostHog.reloadFeatureFlags()
```