Home > Software design >  Why is In-app updates doesn't update the app as expected?
Why is In-app updates doesn't update the app as expected?

Time:10-14

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
            )
        }
    }

}

CodePudding user response:

You have navigation logic inside postDelayed. You are explicitly navigating to new activity after 2500 ms. So the code is doing exactly what you wrote.

If you want to navigate after checking for the update then you probably should move navigation logic to addOnSuccessListener although it does seems like a hack too. Either way i wouldn't do this check in the splash screen but in some more "static" screen that does not have navigation on timer set

CodePudding user response:

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()
        }
    }

  }
}
  • Related