Implemented in-app-update feature in my app. The app asks for update when there is an update available. When i hit the update button it goes to download screen and the app gets a restart after the download is complete with result code = Result_ok. But it again pop up with update available. What could be the reason behind this.
I have used the documentation for reference. https://developer.android.com/guide/playcore/in-app-updates
Also it is immediate update.
I tried debugging for activity results and logged down result code = RESULT_OK.
Gradle files :-
implementation 'com.google.android.play:app-update:2.0.0'
implementation 'com.google.android.play:app-update-ktx:2.0.0'
var appUpdateManager: AppUpdateManager? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.dashboard_navigation)
sharedPreference = SharedPreference(this)
appUpdateManager = AppUpdateManagerFactory.create(this)
checkUpdate()
}
private fun checkUpdate() {
// Returns an intent object that you use to check for an update.
val appUpdateInfoTask = appUpdateManager?.appUpdateInfo
// Checks that the platform will allow the specified type of update.
Log.d(TAG, "Checking for updates")
appUpdateInfoTask?.addOnSuccessListener { appUpdateInfo ->
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)
) {
// Request the update.
try {
appUpdateManager!!.startUpdateFlowForResult(
appUpdateInfo,
AppUpdateType.IMMEDIATE,
this,
MY_REQUEST_CODE
)
Toast.makeText(this, "Update Available", Toast.LENGTH_SHORT).show()
Log.e(TAG, "Update available")
} catch (e: Exception) {
e.printStackTrace()
Toast.makeText(this, "Something went wrong.Contact Admin", Toast.LENGTH_SHORT)
.show()
}
} else {
Log.e(TAG, "No Update available")
}
}
appUpdateInfoTask?.addOnFailureListener {
Log.e(TAG, "checkUpdate: $it")
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == MY_REQUEST_CODE) {
when (resultCode) {
Activity.RESULT_OK -> {
Log.d(TAG, "" + "Result Ok")
// handle user's approval
Log.d(TAG, "onActivityResult: update button clicked")
}
Activity.RESULT_CANCELED -> {
{
//if you want to request the update again just call checkUpdate()
}
Log.d(TAG, "" + "Result Cancelled")
// handle user's rejection }
}
ActivityResult.RESULT_IN_APP_UPDATE_FAILED -> {
//if you want to request the update again just call checkUpdate()
Log.d(TAG, "" + "Update Failure")
// handle update failure
checkUpdate()
}
}
}
}