From 4068cd72ca0816146ffb636b63eba1bb7856480e Mon Sep 17 00:00:00 2001 From: Dominik Dzienia Date: Thu, 23 Jul 2020 12:59:42 +0200 Subject: [PATCH] Fix crash when preferences import list is opened for the second time (due to buggy implementation of ActivityResult and Contracts in early androidx) * updated to newest version of fragments where bug is fixed * catched exception and inform user with Toast * refactor duplicated code in ImportExportPrefs --- app/build.gradle | 1 + .../general/maintenance/ImportExportPrefs.kt | 17 ++++++++++------- build.gradle | 1 + 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 8b9a0adc36..5e822b11cc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -257,6 +257,7 @@ dependencies { implementation 'androidx.percentlayout:percentlayout:1.0.0' implementation "androidx.preference:preference-ktx:1.1.1" implementation "androidx.activity:activity-ktx:${activityVersion}" + implementation "androidx.fragment:fragment:${fragmentVersion}" implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'com.google.android.material:material:1.1.0' diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/ImportExportPrefs.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/ImportExportPrefs.kt index 565b33b77b..b839e31bef 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/ImportExportPrefs.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/ImportExportPrefs.kt @@ -215,12 +215,7 @@ class ImportExportPrefs @Inject constructor( fun importSharedPreferences(fragment: Fragment) { fragment.activity?.let { fragmentAct -> - val callForPrefFile = fragmentAct.registerForActivityResult(PrefsFileContract()) { - it?.let { - importSharedPreferences(fragmentAct, it) - } - } - callForPrefFile.launch(null) + importSharedPreferences(fragmentAct) } } @@ -230,7 +225,15 @@ class ImportExportPrefs @Inject constructor( importSharedPreferences(activity, it) } } - callForPrefFile.launch(null) + + try { + callForPrefFile.launch(null) + } catch (e: IllegalArgumentException) { + // this exception happens on some early implementations of ActivityResult contracts + // when registered and called for the second time + ToastUtils.errorToast(activity, "Please go back to main screen and try again.") + log.error(TAG, "Internal android framework exception", e) + } } private fun importSharedPreferences(activity: Activity, importFile: PrefsFile) { diff --git a/build.gradle b/build.gradle index acf667df6d..89449cca58 100644 --- a/build.gradle +++ b/build.gradle @@ -12,6 +12,7 @@ buildscript { dagger_version = '2.28.1' coroutinesVersion = '1.3.7' activityVersion = '1.2.0-alpha06' + fragmentVersion = '1.3.0-alpha07' ormLiteVersion = "4.46" } repositories {