Skip to content

Commit 135e1ad

Browse files
Add in-app review prompt after session threshold
1 parent 4fd4d9a commit 135e1ad

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

app/src/main/java/com/d4rk/androidtutorials/java/ui/screens/main/MainActivity.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import com.d4rk.androidtutorials.java.ui.screens.support.SupportActivity;
4242
import com.d4rk.androidtutorials.java.utils.ConsentUtils;
4343
import com.d4rk.androidtutorials.java.utils.EdgeToEdgeDelegate;
44+
import com.d4rk.androidtutorials.java.utils.ReviewHelper;
4445
import com.google.android.gms.ads.AdRequest;
4546
import com.google.android.gms.ads.MobileAds;
4647
import com.google.android.material.navigation.NavigationBarView;
@@ -156,6 +157,8 @@ public void handleOnBackPressed() {
156157
}
157158
}
158159
});
160+
161+
checkInAppReview();
159162
}
160163

161164
private void setupActionBar() {
@@ -333,7 +336,22 @@ private void checkForFlexibleOrImmediateUpdate() {
333336
Snackbar.LENGTH_LONG
334337
).show();
335338
}
336-
});
339+
});
340+
}
341+
342+
private void checkInAppReview() {
343+
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
344+
int sessionCount = prefs.getInt(getString(R.string.key_session_count), 0);
345+
boolean hasPrompted = prefs.getBoolean(getString(R.string.key_has_prompted_review), false);
346+
347+
ReviewHelper.launchInAppReviewIfEligible(
348+
this,
349+
sessionCount,
350+
hasPrompted,
351+
() -> prefs.edit().putBoolean(getString(R.string.key_has_prompted_review), true).apply()
352+
);
353+
354+
prefs.edit().putInt(getString(R.string.key_session_count), sessionCount + 1).apply();
337355
}
338356

339357
private void startImmediateUpdate(AppUpdateInfo appUpdateInfo) {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.d4rk.androidtutorials.java.utils;
2+
3+
import android.app.Activity;
4+
5+
import com.google.android.play.core.review.ReviewInfo;
6+
import com.google.android.play.core.review.ReviewManager;
7+
import com.google.android.play.core.review.ReviewManagerFactory;
8+
9+
/**
10+
* Utility class for launching Google Play in-app reviews.
11+
*/
12+
public final class ReviewHelper {
13+
14+
private ReviewHelper() {
15+
// Utility class
16+
}
17+
18+
public static void launchInAppReviewIfEligible(Activity activity,
19+
int sessionCount,
20+
boolean hasPromptedBefore,
21+
Runnable onReviewLaunched) {
22+
if (sessionCount < 3 || hasPromptedBefore) {
23+
return;
24+
}
25+
launchReview(activity, onReviewLaunched);
26+
}
27+
28+
public static void forceLaunchInAppReview(Activity activity) {
29+
launchReview(activity, null);
30+
}
31+
32+
private static void launchReview(Activity activity, Runnable onReviewLaunched) {
33+
ReviewManager reviewManager = ReviewManagerFactory.create(activity);
34+
reviewManager.requestReviewFlow()
35+
.addOnCompleteListener(task -> {
36+
if (task.isSuccessful()) {
37+
ReviewInfo reviewInfo = task.getResult();
38+
reviewManager.launchReviewFlow(activity, reviewInfo)
39+
.addOnCompleteListener(flow -> {
40+
if (onReviewLaunched != null) {
41+
onReviewLaunched.run();
42+
}
43+
});
44+
}
45+
});
46+
}
47+
}

app/src/main/res/values/keys.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@
2121
<string name="key_consent_ad_user_data" translatable="false">consent_ad_user_data</string>
2222
<string name="key_consent_ad_personalization" translatable="false">consent_ad_personalization</string>
2323
<string name="key_onboarding_complete" translatable="false">onboarding_complete</string>
24+
<string name="key_session_count" translatable="false">session_count</string>
25+
<string name="key_has_prompted_review" translatable="false">has_prompted_review</string>
2426
</resources>

0 commit comments

Comments
 (0)