Can I launch Play Store Review Flow from a Fragment?

43 views Asked by At

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.

1

There are 1 answers

0
Hiren Rafaliya On

You need to pass the current host Activity in the ReviewManagerFactory.create().

To get the current activity inside a Fragment you can use getActivity() or requireActivity() which will return the current host Activity of the Fragment.

In your case it should like this -

// get activity instance
Activity currentActivity = getActivity(); // or requireActivity(); for non-null return type.

// create manager
ReviewManagerFactory manager = ReviewManagerFactory.create(currentActivity);

// launch review
Task<Void> flow = manager.launchReviewFlow(currentActivity, reviewInfo);