Skip to content

Commit

Permalink
fix: registerForActivityResult 사용 시 IllegalStateException 오류 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Taewan-P committed Feb 26, 2024
1 parent 6f792d6 commit 83240a2
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions android/app/src/main/java/app/priceguard/ui/home/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.IntentSenderRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
Expand All @@ -24,6 +26,7 @@ import app.priceguard.ui.util.openNotificationSettings
import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.common.GoogleApiAvailability
import com.google.android.material.snackbar.Snackbar
import com.google.android.play.core.appupdate.AppUpdateManager
import com.google.android.play.core.appupdate.AppUpdateManagerFactory
import com.google.android.play.core.appupdate.AppUpdateOptions
import com.google.android.play.core.install.model.AppUpdateType
Expand All @@ -36,15 +39,17 @@ class HomeActivity : AppCompatActivity() {

private lateinit var binding: ActivityHomeBinding
private lateinit var snackbar: Snackbar
private val appUpdateManager = AppUpdateManagerFactory.create(this)
private lateinit var appUpdateManager: AppUpdateManager
private val updateType = AppUpdateType.IMMEDIATE
private lateinit var activityResultLauncher: ActivityResultLauncher<IntentSenderRequest>

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
this.applySystemNavigationBarColor(SystemNavigationColorState.BOTTOM_NAVIGATION)
binding = ActivityHomeBinding.inflate(layoutInflater)
setContentView(binding.root)

setupAppUpdate()
enqueueWorker()
initSnackBar()
checkForGooglePlayServices()
Expand Down Expand Up @@ -137,18 +142,29 @@ class HomeActivity : AppCompatActivity() {
}
}

private fun checkAppUpdates() {
val appUpdateInfoTask = appUpdateManager.appUpdateInfo
val activityResultLauncher = registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { result: ActivityResult ->
when (result.resultCode) {
RESULT_CANCELED -> {
Toast.makeText(this, getString(R.string.update_cancel_warning), Toast.LENGTH_LONG).show()
}
com.google.android.play.core.install.model.ActivityResult.RESULT_IN_APP_UPDATE_FAILED -> {
Toast.makeText(this, getString(R.string.update_failed), Toast.LENGTH_LONG).show()
private fun setupAppUpdate() {
appUpdateManager = AppUpdateManagerFactory.create(this)
activityResultLauncher =
registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { result: ActivityResult ->
when (result.resultCode) {
RESULT_CANCELED -> {
Toast.makeText(
this,
getString(R.string.update_cancel_warning),
Toast.LENGTH_LONG
).show()
}

com.google.android.play.core.install.model.ActivityResult.RESULT_IN_APP_UPDATE_FAILED -> {
Toast.makeText(this, getString(R.string.update_failed), Toast.LENGTH_LONG)
.show()
}
}
}
}
}

private fun checkAppUpdates() {
val appUpdateInfoTask = appUpdateManager.appUpdateInfo

appUpdateInfoTask.addOnSuccessListener { info ->
if (info.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE &&
Expand Down

0 comments on commit 83240a2

Please sign in to comment.