From 83240a2937ae1fbba40f17f8dbf5de66954ef16a Mon Sep 17 00:00:00 2001 From: Taewan Park Date: Mon, 26 Feb 2024 21:48:57 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20registerForActivityResult=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=20=EC=8B=9C=20IllegalStateException=20=EC=98=A4?= =?UTF-8?q?=EB=A5=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/priceguard/ui/home/HomeActivity.kt | 38 +++++++++++++------ 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/android/app/src/main/java/app/priceguard/ui/home/HomeActivity.kt b/android/app/src/main/java/app/priceguard/ui/home/HomeActivity.kt index 9b3c427..31a98fa 100644 --- a/android/app/src/main/java/app/priceguard/ui/home/HomeActivity.kt +++ b/android/app/src/main/java/app/priceguard/ui/home/HomeActivity.kt @@ -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 @@ -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 @@ -36,8 +39,9 @@ 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 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -45,6 +49,7 @@ class HomeActivity : AppCompatActivity() { binding = ActivityHomeBinding.inflate(layoutInflater) setContentView(binding.root) + setupAppUpdate() enqueueWorker() initSnackBar() checkForGooglePlayServices() @@ -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 &&