Use this file to discover all available pages before exploring further.
The TriggersProvider enables real-time reactive programming patterns by allowing you to observe cohort membership changes and trigger actions when users enter or exit specific cohorts. This is powerful for personalizing user experiences, A/B testing, and dynamic content delivery.
Create a TriggersProvider instance from your Permutive SDK instance:
import com.permutive.android.TriggersProviderclass MyActivity : AppCompatActivity() { private val permutive by lazy { (application as MyApplication).permutive } private val triggersProvider: TriggersProvider by lazy { permutive.triggersProvider() } // Use the triggers provider...}
💡 Lifecycle ManagementYou can create any number of TriggersProvider instances. They’re lightweight and don’t maintain heavy state.
import com.permutive.android.TriggerActionclass PersonalizedActivity : AppCompatActivity() { private var premiumContentTrigger: TriggerAction? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_personalized) val triggersProvider = permutive.triggersProvider() // Monitor if user is in the "premium_readers" cohort premiumContentTrigger = triggersProvider.triggerAction("premium_readers") { isInCohort -> if (isInCohort) { showPremiumContent() } else { showStandardContent() } } } override fun onDestroy() { super.onDestroy() // Clean up the trigger when the activity is destroyed premiumContentTrigger?.close() } private fun showPremiumContent() { // Update UI to show premium features binding.premiumBanner.visibility = View.VISIBLE binding.subscriptionPrompt.visibility = View.GONE } private fun showStandardContent() { // Update UI for standard users binding.premiumBanner.visibility = View.GONE binding.subscriptionPrompt.visibility = View.VISIBLE }}
class AdTargetingActivity : AppCompatActivity() { private var gamActivationTrigger: TriggerAction? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val triggersProvider = permutive.triggersProvider() // Monitor Google Ad Manager activations gamActivationTrigger = triggersProvider.cohortActivations("dfp") { cohorts -> Log.d("Permutive", "User has ${cohorts.size} active GAM cohorts: $cohorts") updateAdTargeting(cohorts) } } override fun onDestroy() { super.onDestroy() gamActivationTrigger?.close() } private fun updateAdTargeting(cohorts: List<String>) { // Use cohorts for ad targeting }}
class MyActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Creates a new trigger every time, never closes the old ones triggersProvider.triggerAction("cohort_id") { /* ... */ } } // Memory leak! Triggers never closed}
The TriggersProvider and TriggerAction are thread-safe. Callbacks are delivered on a background thread, so if you need to update UI, use appropriate threading mechanisms:
private var trigger: TriggerAction? = nulloverride fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) trigger = triggersProvider.triggerAction("cohort_id") { isInCohort -> // Callback is on background thread lifecycleScope.launch(Dispatchers.Main) { // Update UI on main thread if (isInCohort) { textView.text = "User is in cohort" } } }}
📘 Why the Change?The SDK now supports Classification Model cohorts which use string IDs. The new APIs support both traditional integer-based cohorts and newer string-based cohorts seamlessly.
Problem: App memory usage grows over time.Cause:TriggerAction instances not being closed.Solution: Always call close() on triggers in appropriate lifecycle methods (usually onDestroy() or onCleared()).
Callbacks on Wrong Thread
Problem: Crash when trying to update UI from callback.Cause: Callbacks execute on background threads.Solution: Use Handler, coroutines, or runOnUiThread() to update UI (see Thread Safety section above).