Created
June 27, 2022 19:40
-
-
Save wajahatkarim3/7eb876457e70128067a1187d9d3e18bd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
var reviewManager: ReviewManager? = null | |
var reviewInfo: ReviewInfo? = null | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
getReviewInfo(); | |
someButton.setOnClickListener { | |
startReviewFlow() | |
} | |
} | |
/** | |
* Call this method at app start etc to pre-cache the reviewInfo object to use to show | |
* in-app review dialog later. | |
*/ | |
private fun getReviewInfo() { | |
reviewManager = ReviewManagerFactory.create(applicationContext) | |
val manager = reviewManager?.requestReviewFlow() | |
manager?.addOnCompleteListener { task: Task<ReviewInfo?> -> | |
if (task.isSuccessful) { | |
reviewInfo = task.result | |
} else { | |
Toast.makeText( | |
applicationContext, | |
"In App ReviewFlow failed to start", | |
Toast.LENGTH_LONG | |
).show() | |
} | |
} | |
} | |
/** | |
* Call this method when you want to show the in-app rating dialog | |
*/ | |
fun startReviwFlow() { | |
if (reviewInfo != null) { | |
val flow = reviewManager!!.launchReviewFlow(this, reviewInfo!!) | |
flow.addOnCompleteListener { | |
Toast.makeText( | |
applicationContext, | |
"Rating complete", | |
Toast.LENGTH_LONG | |
).show() | |
} | |
} else { | |
Toast.makeText(applicationContext, "Rating failed", Toast.LENGTH_LONG).show() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment