Android with Jetpack Compose

Process a transaction with an Android app built with Jetpack Compose

Jetpack Compose is a new modern toolkit for building native UI for Android. One major difference in Jetpack Compose is the absence of Activity. The purchasePackage() and purchasePackageWith() functions accept an Activity as the first parameter but an Activity is not easily accessible in a @Composable function.

To get around this, you can create an extension function to recursively find the nearest Activity from your Jetpack Compose context.

fun Context.findActivity(): Activity? = when (this) {
    is Activity -> this
    is ContextWrapper -> baseContext.findActivity()
    else -> null
}

So example below shows how to check the nullable Activity? returned by LocalContext.current.findActivity() and pass it into purchase(package:).

LocalContext.current.findActivity()?.let { activity ->
	Purchases.sharedInstance.purchaseWith(
		PurchaseParams.Builder(this, aPackage).build(),
    onError = { error, userCancelled -> /* No purchase */ },
    onSuccess = { storeTransaction, customerInfo ->
			if (customerInfo.entitlements["my_entitlement_identifier"]?.isActive == true) {
				// Unlock that great "pro" content
			}
    }
	)
}