Question

Use Application Context for making a purchase

  • 15 July 2022
  • 1 reply
  • 238 views

Badge +4

Hello,

I am in the process of migrating one of our apps to Jetpack Compose which is using RevenueCat for processing purchases. Earlier, the code to make the purchase was inside the Fragment and hence I was able to pass the Activity Context while calling the purchasePackageWith() function.

 

However, now the code to make the purchase is moved to the repository where I am injecting the Context using Dagger Hilt. Since, the injected code is an Application Context I am unable to call the purchasePackageWith() function.

 

I tried getting the Activity Context using the below code but it doesn’t work. 

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

 

I can pass the Activity Context from the composable screen to the repository via ViewModel but that may leak the activity. So, is there any proper approach to do the same?


1 reply

Userlevel 3
Badge +5

@aculix Hello! 👋 

Just wanted to let you know (and anybody else that finds this) that our docs have been updated to have a new page for Jetpack Compose - https://www.revenuecat.com/docs/android-with-jetpack-compose

Reply