Khurria Zafar
Khurria Zafar

Reputation: 43

how to implement in-app review API into android app with onResume?

The description states on a topic when to ask for a review:

Code is working fine when I put it into button Click Listener, But I want to show inApp review Dialog on onResume() . When i call this method on onResume it calls again and again and never stop. i found it by putting Log in addOnCompleteListener method.

   @Override
protected void onResume() {
    Log.d("inAppReview", "reviewed");

    if (reviewInfo != null) {
        ReviewManager manager = ReviewManagerFactory.create(this);
        Task<ReviewInfo> request = manager.requestReviewFlow();
        request.addOnCompleteListener(task -> {
            try {
                if (task.isSuccessful()) {
                    // We can get the ReviewInfo object
                    ReviewInfo reviewInfo = task.getResult();
                    Task<Void> flow = manager.launchReviewFlow(this, reviewInfo);
                    flow.addOnCompleteListener(task2 ->
                    {
                        // 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.
                        //  utility.logMessageAsync(activity, "In-app review returned.");
                        Log.d("inAppReview", "review successful");

                    });

                } else {
                    // There was some problem, continue regardless of the result.
                    // goToAppPage(activity);
                }
            } catch (Exception ex) {
                Log.d("inAppReview", "exception");
                // utility.logExceptionAsync(activity, "Exception from openReview():", ex);
            }
        });
        super.onResume();
    } else {
        Log.d("inAppReview", "Error While Reviewing");
    }
    super.onResume();}

Below I am attaching this picture of my Logcat

enter image description here

How to call this method only once?

Any help would be appreciated

Upvotes: 0

Views: 751

Answers (2)

himanshu1496
himanshu1496

Reputation: 1921

Have a boolean to check if app is reviewed something like below:

 boolean isAppReviewed = false;

 @Override
 protected void onResume() {
 if(isAppReviewed) {
     super.onResume();
     return;
 }
 Log.d("inAppReview", "reviewed");

 if (reviewInfo != null) {
    ReviewManager manager = ReviewManagerFactory.create(this);
    Task<ReviewInfo> request = manager.requestReviewFlow();
    request.addOnCompleteListener(task -> {
        try {
            if (task.isSuccessful()) {
                // We can get the ReviewInfo object
                ReviewInfo reviewInfo = task.getResult();
                Task<Void> flow = manager.launchReviewFlow(this, reviewInfo);
                flow.addOnCompleteListener(task2 ->
                {
                    // 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.
                    //  utility.logMessageAsync(activity, "In-app review returned.");
                    Log.d("inAppReview", "review successful");
                    isAppReviewed = true;
                });

            } else {
                // There was some problem, continue regardless of the result.
                // goToAppPage(activity);
            }
        } catch (Exception ex) {
            Log.d("inAppReview", "exception");
            // utility.logExceptionAsync(activity, "Exception from openReview():", ex);
        }
    });
    super.onResume();
} else {
    Log.d("inAppReview", "Error While Reviewing");
}
super.onResume();
}

You can update isAppReviewed to true whenever its suitable to stop showing review, in my example I am doing it inside flow.addOnCompleteListener. But you can move it based on your business logic.

Upvotes: 1

Phantom Lord
Phantom Lord

Reputation: 701

Simply don't put in onResume. It's a bad practice

Upvotes: 0

Related Questions