Using the Android SDK
Android SDK information
SDK Initialization
It takes the AppFlags Android SDK about 100ms to fetch the user's Feature Flags from the AppFlags edge servers.
You can use the AppFlagsClient
immediately after creating it. The Feature Flags it returns will have default values until the SDK has initialized. See Default Values to learn more.
Automatic Updates
The AppFlags Android SDK listens for updates to your Feature Flags. When you update Feature Flags in the AppFlags dashboard, the AppFlags client is immediately notified and updates with new Feature Flag values. See Feature Flag Value Updates to learn more.
Getting the AppFlagsClient
In the Android SDK, AppFlagsClient
is a singleton.
For convenience, you can get the existing AppFlagsClient
anywhere in your code by calling AppFlagsClient.getClient()
:
val appFlagsClient: AppFlagsClient = AppFlagsClient.getClient()
Getting a Feature Flag
To get a Feature Flag, call the relevant getFlag
function with the flag_key
and a default_value
.
val flag: AppFlagsFlag<Boolean> = appFlagsClient.getBooleanFlag("flag_key", default_value)
The returned AppFlagsFlag
object contains the current feature flag value in the value
property:
val flagValue: Boolean = flag.value
Default values
If the SDK has not initialized yet or the Feature Flag specified by the flag_key
does not exist, then the returned AppFlagsFlag
will contain the default value that you provided.
If you are not 100% sure that the SDK has already initialized, you should subscribe to Feature Flag updates to get notified of the flag's actual value once the SDK has initialized. See Subscribe to Feature Flag Updates.
Variation types
There are getFlag
functions for boolean
, number
, and string
Feature Flags:
val booleanFlag: AppFlagsFlag<Boolean> = appFlagsClient.getBooleanFlag("boolean_flag", false)
val numberFlag: AppFlagsFlag<Double> = appFlagsClient.getNumberFlag("number_flag", 0.0)
val stringFlag: AppFlagsFlag<String> = appFlagsClient.getStringFlag("string_flag", "default")
Feature Flag Value Updates
The value
property in AppFlagsFlag
will automatically update in the following situations:
- The SDK finishes initializing from the AppFlags servers
- The Feature Flag is modified in the AppFlags dashboard
- The current user has been changed
Your code will likely not read the new value automatically. If you wish to get notified when the Feature Flag updates, then Subscribe to Feature Flag Updates.
Subscribe to Feature Flag Updates
To subscribe to Realtime Updates on a Feature Flag, add an onUpdate
callback:
flag.onUpdate { updatedFlag ->
run {
... = updatedFlag.value
}
}
In the example above, the value in the existing flag
object has updated, but we provide an updatedFlag
to the callback for convenience.
Update the User
The user of your application may change as they login, logout, or update their information.
To update the user in the AppFlags SDK, construct a new AppFlagsUser
and call updateUser
:
val newUser: AppFlagsUser = AppFlagsUser("newUserKey")
AppFlagsClient.updateUser(newUser)
The AppFlags SDK will fetch updated Feature Flags and update any existing AppFlagsFlag
instances if necessary.
Example Repository
For a full example of using AppFlags in an Android application, see https://github.com/AppFlags/example-android.
For ease of use, we suggest creating a ViewModel that contains your AppFlags Feature Flags. The Android example application contains an example of this.