I'm reworking my app and in my new app flow the best place to call for a Google Play Store review is in a fragment. Before I was able to launch the review code with no problems when it was in an activity. However, now that I've copied the code over to a fragment I have 2 sticking points I'm not sure how to resolve.
- Let's call the prior activity "FormerActivity"
- Let's call the new fragment "NewFragment"
So, the code was like so (there are 2 methods):
private void activateReviewInfo() {
manager = ReviewManagerFactory.create(FormerActivity.this);
Task<ReviewInfo> request = manager.requestReviewFlow();
request.addOnCompleteListener(task -> {
if(task.isSuccessful()) {
reviewInfo = task.getResult();
}
});
}
private void startReviewFlow() {
if(reviewInfo != null) {
Task<Void> flow = manager.launchReviewFlow(FormerActivity.this, reviewInfo);
flow.addOnCompleteListener(task -> {
});
} else {
// not creating a review because the object is null
}
}
The issues are:
manager = ReviewManagerFactory.create(FormerActivity.this);
&&
Task<Void> flow = manager.launchReviewFlow(FormerActivity.this, reviewInfo);
I've tried all types of combinations to replace the Activity name with the Fragment name (e.g. NewFragment.this, this, NewFragment and so on). I know I'm missing some fundamental here in my understanding. I'm hoping that somehow these methods will be able to accept a fragment and not just an activity. Any assist would be appreciated, thanks.
You need to pass the current host
Activityin theReviewManagerFactory.create().To get the current activity inside a
Fragmentyou can usegetActivity()orrequireActivity()which will return the current hostActivityof theFragment.In your case it should like this -