Using the guide from Google, we get the following Kotlin code to launch the in-app review process...
val activity = ??? // what should we use for activity?
val manager = ReviewManagerFactory.create(activity)
val request = manager.requestReviewFlow()
request.addOnCompleteListener { task ->
if (task.isSuccessful) {
// We got the ReviewInfo object
val reviewInfo = task.result
val flow = manager.launchReviewFlow(activity, reviewInfo)
flow.addOnCompleteListener { _ ->
// The flow has finished. The API does not indicate whether the user
// reviewed or not, or even whether the review dialog was shown. Thus, no
// matter the result, we continue our app flow.
}
} else {
// There was some problem, log or handle the error code.
@ReviewErrorCode val reviewErrorCode = (task.getException() as ReviewException).errorCode
}
}
https://developer.android.com/guide/playcore/in-app-review/kotlin-java
However, I can't find any guidance on whether a new activity should be created, or whether the currently running activity should be used.
I would like to request the review after users have posted something in my app. However, it's unclear where I fit this into my usual control flow between my ViewModel and Composables.
Thanks for the help!
You can use current activity context.
Directly call review from your composable function and cast local context as activity.
Something like this:
fun launchInAppReview(context: Context) {
val reviewManager = ReviewManagerFactory.create(context as Activity)
val requestReviewFlow = reviewManager.requestReviewFlow()
requestReviewFlow.addOnCompleteListener { request ->
if (request.isSuccessful) {
val reviewInfo = request.result
reviewManager.launchReviewFlow(context, reviewInfo)
} else {
Timber.e(request.exception?.localizedMessage)
}
}
}
And just call above function from composable:
@Composable
private fun SampleApp(viewModel: MyViewModel) {
val state by viewModel.state.collectAsState()
val context = LocalContext.current
LaunchedEffect(key1 = state.showInAppReviewPrompt) {
if (state.showInAppReviewPrompt) {
launchInAppReview(context)
}
}
}