---
title: OptimizelyUserContext for the Android SDK
slug: feature-experimentation/docs/optimizelyusercontext-android
docTags: 
createdAt: 2026-08-05T20:39:58.000Z
---

The `OptimizelyUserContext` object lets you make flag decisions and track events for a user context that you have already created using the [Create User Context](docId\:gR9wq-XMrQQOtGcWhpyqQ) method.

## OptimizelyUserContext minimum SDK version

`OptimizelyUserContext` is supported on the Optimizely Feature Experimentation Android SDK v3.9 and higher.

### Forced decision methods minimum SDK version

`setForcedDecision()`, `getForcedDecision()`, `removeForcedDecision()` and `removeAllForcedDecisions()` methods are supported on v3.13 and higher.

### Real-Time Audiences for Feature Experimentation minimum SDK version

`fetchQualifiedSegments()` and `isQualifiedFor()` methods are supported on v4.0.0 and higher.

## OptimizelyUserContext definition

The following code shows the object definition for `OptimizelyUserContext`:

```kotlin
class OptimizelyUserContext(optimizely: Optimizely,
                            userId: String,
                            attributes: Map<String, Any?>?) {
    val userId: String
    val attributes: MutableMap<String?, Any?>

    // set an attribute for the user
    fun setAttribute(key: String, value: Any?)

    // make a decision about which flag variation the user buckets into for the flag key
    fun decide(key: String,
               options: List<OptimizelyDecideOption?>? = emptyList()): OptimizelyDecision

    // make decisions about which flag variations the user buckets into for flag keys
    fun decideForKeys(keys: List<String?>?,
                      options: List<OptimizelyDecideOption?>? = emptyList()): Map<String, OptimizelyDecision>

    // make decisions about which flag variations the user buckets into for all flags
    fun decideAll(options: List<OptimizelyDecideOption?>? = emptyList()): Map<String, OptimizelyDecision>

    // track user event
    fun trackEvent(eventName: String,
                   eventTags: Map<String?, *>? = emptyMap<String?, Any>())

    // sets a forced decision (variationKey) for a given decision context
    fun setForcedDecision(optimizelyDecisionContext: OptimizelyDecisionContext,
                          optimizelyForcedDecision: OptimizelyForcedDecision): Boolean

    // returns the forced decision (variationKey) for a given decision context
    fun getForcedDecision(optimizelyDecisionContext: OptimizelyDecisionContext): OptimizelyForcedDecision?

    // removes the forced decision (variationKey) for a given decision context
    fun removeForcedDecision(optimizelyDecisionContext: OptimizelyDecisionContext): Boolean

    // removes all the forced decisions (variationKeys) for the user context
    fun removeAllForcedDecisions(): Boolean
  
    //
    // The following methods require Real-Time Audiences for Feature Experimentation enabled. 
    // See note below code sample. 
    //

    // An array of audience names that the user is qualified for. 
    // The result of **fetchQualifiedSegments()** is saved here. 
    // Can be null if not properly updated with fetchQualifiedSegments(). 
    // 
    //  You can read and write directly to the qualified segments list. 
    //  This lets you bypass the remote fetching process from 
    //  Optimizely Data Platform or use your own fetching service.   
    var qualifiedSegments: MutableList<String>?

    // Fetch all qualified segments for the user context. 
    //
    // The segments fetched are saved in **qualifiedSegments** and can be accessed any time.
    // On failure, **qualifiedSegments** is null.
    //
    // Synchronous: returns true if the fetch succeeded.
    fun fetchQualifiedSegments(segmentOptions: List<ODPSegmentOption> = emptyList()): Boolean
    //
    // Asynchronous: fetches in a separate thread and calls the callback with true on success.
    fun fetchQualifiedSegments(callback: ODPSegmentCallback,
                               segmentOptions: List<ODPSegmentOption> = emptyList())


    // Check if the user is qualified for the given segment. 
    // - Parameter segment: the segment name to check qualification for.
    // - Returns: true if qualified.
    fun isQualifiedFor(segment: String): Boolean
}
```

```java
public class OptimizelyUserContext {
    public String getUserId()
    public Map<String, Object> getAttributes()

    public OptimizelyUserContext(Optimizely optimizely, 
                                 String userId, 
                                 Map<String, Object> attributes)
      
    // set an attribute for the user
    public void setAttribute(String key, Object value)
    
    // make a decision about which flag variation the user buckets into for the flag key
    public OptimizelyDecision decide(String key, 
                                     List<OptimizelyDecideOption> options)
    
    // make decisions about which flag variations the user buckets into for flag keys  
    public Map<String, OptimizelyDecision> decideForKeys(List<String> keys, 
                                      List<OptimizelyDecideOption> options)
    
    // make decisions about which flag variations the user buckets into for all flags  
    public Map<String, OptimizelyDecision> decideAll(List<OptimizelyDecideOption> options)
    
    // track user event  
    public void trackEvent(String eventName, Map<String, ?> eventTags)
  
    // sets a forced decision (variationKey) for a given decision context
    public Boolean setForcedDecision(OptimizelyDecisionContext context, OptimizelyForcedDecision decision)

    // returns the forced decision (variationKey) for a given decision context
    public OptimizelyForcedDecision getForcedDecision(OptimizelyDecisionContext context)

    // removes the forced decision (variationKey) for a given decision context
    public boolean removeForcedDecision(OptimizelyDecisionContext context)

    // removes all the forced decisions (variationKeys) for the user context
    public boolean removeAllForcedDecisions()
    
      //
    // The following methods require Real-Time Audiences for Feature Experimentation 
    // integration enabled. See note below code sample. 
    //

    // An array of segment names that the user is qualified for. 
    // The result of **fetchQualifiedSegments()** is saved here. 
    // Can be null if not properly updated with fetchQualifiedSegments(). 
    // 
    //  You can read and write directly to the qualified segments list. 
    //  This allows for bypassing the remote fetching process from ODP 
    //  or for utilizing your own fetching service.   
    public List<String> getQualifiedSegments()
    public void setQualifiedSegments(List<String> qualifiedSegments)

    // Fetch all qualified segments for the user context. 
    //
    // The segments fetched are saved in **qualifiedSegments** and can be accessed any time
    // with getQualifiedSegments(). On failure, **qualifiedSegments** is null.
    //
    // Synchronous: returns true if the fetch succeeded.
    public Boolean fetchQualifiedSegments()
    public Boolean fetchQualifiedSegments(List<ODPSegmentOption> segmentOptions)
    //
    // Asynchronous: fetches in a separate thread and calls
    // callback.onCompleted(Boolean isFetchSuccessful) when results are available.
    public void fetchQualifiedSegments(ODPSegmentCallback callback)
    public void fetchQualifiedSegments(ODPSegmentCallback callback,
                                       List<ODPSegmentOption> segmentOptions)


    // Check if the user is qualified for the given segment. 
    // - Parameter segment: the segment name to check qualification for.
    // - Returns: true if qualified.
    public boolean isQualifiedFor(String segment)
    
}
```

:::hint{type="info"}
**Note**
You must first enable the [Real-Time Audiences for Feature Experimentation](docId:6UBBxxRv331nBIVZ2lun8) integration to use the `getQualifiedSegments()`, `setQualifiedSegments()`, `fetchQualifiedSegments()`, and `isQualifiedFor()` methods. See [Real-Time Audiences for Feature Experimentation](docId:6UBBxxRv331nBIVZ2lun8) for information.
:::

## Properties

The following table shows attributes for the `OptimizelyUserContext` object:

| Attribute             | Type   | Comment                                                                                                                                                                                                  |
| --------------------- | ------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| userId                | String | The ID of the user                                                                                                                                                                                       |
| (optional) attributes | Map    | A map of custom key-value pairs specifying attributes for the user that are used for [audience targeting](docId\:UDBvr5a8WHNkyOqzPhOr9). You can pass the map with the user ID when you create the user. |

## Methods

The following table shows methods for the `OptimizelyUserContext` object:

| Method                      | Comment                                                                                                                                                                                                                                                                                                                                                                  |
| --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| setAttribute                | Pass a custom user attribute as a key-value pair to the user context.                                                                                                                                                                                                                                                                                                    |
| decide                      | Returns a decision result for a flag key for a user. The decision result is returned in an OptimizelyDecision object, and contains all data required to deliver the flag rule. See \[Decide methods]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/decide-methods-android#decide)                                                                |
| decideAll                   | Returns decisions for all active (unarchived) flags for a user. See \[Decide methods]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/decide-methods-android#decide-all)                                                                                                                                                                           |
| decideForKeys               | Returns a map of flag decisions for specified flag keys. See \[Decide methods]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/decide-methods-android#decide-for-keys)                                                                                                                                                                             |
| trackEvent                  | Tracks a conversion event (in other words, an action a user takes) for a user. Logs an error message if the specified event key doesn't match any existing events. See \[Track Event]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/track-event-android)                                                                                         |
| setForcedDecision           | Forces a user into a specific variation. See \[Set Forced Decision]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/forced-decision-methods-android#set-forced-decision-method---setforceddecision)                                                                                                                                                |
| getForcedDecision           | Returns what variation the user was forced into. See \[Get Forced Decision]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/forced-decision-methods-android#get-forced-decision-method---getforceddecision)                                                                                                                                        |
| removeForcedDecision        | Removes a user from a specific forced variation. See \[Remove Forced Decision]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/forced-decision-methods-android#remove-forced-decision-method---removeforceddecision)                                                                                                                               |
| removeAllForcedDecisions    | Removes a user from all forced variations. See \[Remove All Forced Decisions]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/forced-decision-methods-android#remove-all-forced-decisions-method---removeallforceddecisions)                                                                                                                       |
| fetchQualifiedSegments \*\* | Fetch all Optimizely Data Platform (ODP) real-time audiences that the user context qualified for. See \[Real-Time Audiences for Feature Experimentation segment qualification methods for the Android SDK]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/real-time-audiences-for-feature-experimentation-segment-qualification-methods-android). |
| isQualifiedFor \*\*         | Check if the user context qualified for a given ODP real-time audience. See \[Real-Time Audiences for Feature Experimentation segment qualification methods for the Android SDK]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/real-time-audiences-for-feature-experimentation-segment-qualification-methods-android).                           |

\*\* Requires [Real-Time Audiences for Feature Experimentation](docId:6UBBxxRv331nBIVZ2lun8).

## Source files

The language and platform source files containing the implementation for the Android SDK are available on [GitHub](https://github.com/optimizely/java-sdk/blob/master/core-api/src/main/java/com/optimizely/ab/Optimizely.java).
