Skip to content

Instantly share code, notes, and snippets.

@dhruvtaneja
Last active May 27, 2019 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhruvtaneja/6978134bd6835fa73bd5788980232299 to your computer and use it in GitHub Desktop.
Save dhruvtaneja/6978134bd6835fa73bd5788980232299 to your computer and use it in GitHub Desktop.
class MainActivity : AppCompatActivity(), InstallStateUpdatedListener {
override fun onStateUpdate(installState: InstallState) {
if (installState.installStatus() == InstallStatus.DOWNLOADED) {
notifyUser()
}
}
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
appUpdateManager = AppUpdateManagerFactory.create(this)
appUpdateManager.registerListener(this)
appUpdateManager.appUpdateInfo.addOnSuccessListener {
if (it.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE &&
it.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) { // check for the type of update flow you want
requestUpdate()
}
}
override fun onDestroy() {
super.onDestroy()
appUpdateManager.unregisterListener(this)
}
private fun notifyUser() {
Snackbar
.make(appVersionTextView, R.string.restart_to_update, Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.action_restart) {
appUpdateManager.completeUpdate()
appUpdateManager.unregisterListener(this)
}
.show()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment