Following is the code inside my SplashActivity.kt
. Whenever there is an update available, mostly it doesn't show at all, but I think that is not because of any mistake in my code but as I learned it takes some time to show the user about the new update. My concern now is that when it shows the in-app update screen, it goes off and the app continues to the next screen and it doesn't ask the user to update the app to continue using the app. Have I made a mistake?
@Suppress("DEPRECATION")
class SplashActivity : AppCompatActivity() {
private lateinit var binding: ActivitySplashBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivitySplashBinding.inflate(layoutInflater)
setContentView(binding.root)
callInAppUpdate()
window.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
)
Handler().postDelayed(
{
val currentUserID = FirestoreClass().getCurrentUserID()
if (currentUserID.isNotEmpty()) {
startActivity(Intent(this@SplashActivity, HomeActivity::class.java))
} else {
startActivity(
Intent(
this@SplashActivity,
LoginActivity::class.java
)
)
}
finish()
},
2500
)
}
public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (data == null) return
if (requestCode == updateRequestCode) {
Toast.makeText(this, "Downloading", Toast.LENGTH_SHORT).show()
if (resultCode != RESULT_OK) {
Log.d("TAG", "update flow failed $resultCode")
}
}
}
private val updateRequestCode = 1612
private fun callInAppUpdate() {
val appUpdateManager = AppUpdateManagerFactory.create(this)
val appUpdateInfoTask = appUpdateManager.appUpdateInfo
appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)
) {
appUpdateManager.startUpdateFlowForResult(
appUpdateInfo, AppUpdateType.IMMEDIATE, this, updateRequestCode
)
}
}
}
}
EDIT:
I moved the postDelayed
to the addOnSuccessListener
, the fun callInAppUpdate()
now look like the following.
private fun callInAppUpdate() {
val appUpdateManager = AppUpdateManagerFactory.create(this)
val appUpdateInfoTask = appUpdateManager.appUpdateInfo
appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)
) {
appUpdateManager.startUpdateFlowForResult(
appUpdateInfo, AppUpdateType.IMMEDIATE, this, updateRequestCode
)
}else{
Handler().postDelayed(
{
val currentUserID = FirestoreClass().getCurrentUserID()
if (currentUserID.isNotEmpty()) {
startActivity(Intent(this@SplashActivity, DashboardActivity::class.java))
} else {
startActivity(
Intent(
this@SplashActivity,
LoginWithPhoneNumberActivity::class.java
)
)
}
finish()
},
2500
)
}
}
}
You could achieve your behaviour like below:
class SplashActivity : AppCompatActivity() {
private val updateRequestCode = 1612
private lateinit var binding: ActivitySplashBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivitySplashBinding.inflate(layoutInflater)
setContentView(binding.root)
callInAppUpdate()
window.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
)
}
public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (data == null) return
if (requestCode == updateRequestCode) {
Toast.makeText(this, "Downloading", Toast.LENGTH_SHORT).show()
if (resultCode != RESULT_OK) {
Log.d("TAG", "update flow failed $resultCode")
navigate()
}
}
}
private fun navigate(){
Handler().postDelayed(
{
val currentUserID = FirestoreClass().getCurrentUserID()
if (currentUserID.isNotEmpty()) {
startActivity(Intent(this@SplashActivity, DashboardActivity::class.java))
} else {
startActivity(
Intent(
this@SplashActivity,
LoginWithPhoneNumberActivity::class.java
)
)
}
finish()
},
2500
)
}
private fun callInAppUpdate() {
val appUpdateManager = AppUpdateManagerFactory.create(this)
val appUpdateInfoTask = appUpdateManager.appUpdateInfo
appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
appUpdateManager.startUpdateFlowForResult(
appUpdateInfo, AppUpdateType.IMMEDIATE, this, updateRequestCode
)
}
else {
navigate()
}
}
}
}