From 1cf5b358287ce6de6c70190e34947640f48cd9fb Mon Sep 17 00:00:00 2001 From: Victor Albertos Gil Date: Wed, 1 Jul 2020 17:35:33 +0200 Subject: [PATCH 1/3] [#2] add LenientNavHostFragment --- .../bottom_nav_watson/BottomNavWatson.kt | 2 +- .../LenientNavHostFragment.kt | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 bottom-nav-watson/src/main/kotlin/bottom_nav_watson/LenientNavHostFragment.kt diff --git a/bottom-nav-watson/src/main/kotlin/bottom_nav_watson/BottomNavWatson.kt b/bottom-nav-watson/src/main/kotlin/bottom_nav_watson/BottomNavWatson.kt index c82c2b6..78526a1 100644 --- a/bottom-nav-watson/src/main/kotlin/bottom_nav_watson/BottomNavWatson.kt +++ b/bottom-nav-watson/src/main/kotlin/bottom_nav_watson/BottomNavWatson.kt @@ -263,7 +263,7 @@ private fun AppCompatActivity.obtainNavHostFragment( // but it starts to corrupt the back stack when Don't keep activities is enabled, which is avoided by // calling NavHostFragment.create(graphResId) and setting as the start destination of the graph -in the xml // declaration- an empty fragment. - val navHostFragment = NavHostFragment.create(graphResId) + val navHostFragment = LenientNavHostFragment.create(graphResId) fragmentManager.beginTransaction() .add(containerId, navHostFragment, fragmentTag) .commitNow() diff --git a/bottom-nav-watson/src/main/kotlin/bottom_nav_watson/LenientNavHostFragment.kt b/bottom-nav-watson/src/main/kotlin/bottom_nav_watson/LenientNavHostFragment.kt new file mode 100644 index 0000000..613ad63 --- /dev/null +++ b/bottom-nav-watson/src/main/kotlin/bottom_nav_watson/LenientNavHostFragment.kt @@ -0,0 +1,36 @@ +package bottom_nav_watson + +import android.os.Bundle +import androidx.navigation.fragment.NavHostFragment + +/** + * We need to subclass NavHostFragment to try/catch the call to super::onDestroyView + * as it throws an IllegalStateException when a deep link is launched starting from version + * 2.3.0 of the navigation component. Specifically, it crashes when the NavHostFragment + * tries to dispose itself by finding its associated NavController and there is none. + */ +class LenientNavHostFragment : NavHostFragment() { + + override fun onDestroyView() { + try { + super.onDestroyView() + } catch (e: IllegalStateException) { + // We only sallow the exception when it contains the message related with the issue that we know. + // Otherwise we rethrow, this is brittle as it ties the implementation to a private reporting message error, + // but we prefer this way to avoid hiding unrelated exceptions. + if (e.message?.contains("does not have a NavController set") == false) { + throw e + } + } + } + + companion object { + // We need to duplicate here the key as it's a private field in the library, which is brittle but it is what it is. + private const val KEY_GRAPH_ID = "android-support-nav:fragment:graphId" + + fun create(graphResId: Int): NavHostFragment = LenientNavHostFragment().apply { + arguments = Bundle().apply { putInt(KEY_GRAPH_ID, graphResId) } + } + } + +} From 2948169731fdfb635b75b136c6f579215a2ca254 Mon Sep 17 00:00:00 2001 From: Victor Albertos Gil Date: Wed, 1 Jul 2020 17:35:48 +0200 Subject: [PATCH 2/3] [#2] bump navigation version --- bottom-nav-watson/build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bottom-nav-watson/build.gradle b/bottom-nav-watson/build.gradle index 0ab3d7e..f5c93f9 100644 --- a/bottom-nav-watson/build.gradle +++ b/bottom-nav-watson/build.gradle @@ -29,6 +29,6 @@ dependencies { implementation 'androidx.core:core-ktx:1.3.0' implementation 'com.google.android.material:material:1.1.0' - implementation 'androidx.navigation:navigation-runtime:2.2.2' - implementation 'androidx.navigation:navigation-fragment:2.2.2' + implementation 'androidx.navigation:navigation-runtime:2.3.0' + implementation 'androidx.navigation:navigation-fragment:2.3.0' } From 84d2beec91c26bcf142b509dda79799eac52666e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Albertos?= Date: Thu, 2 Jul 2020 14:50:26 +0200 Subject: [PATCH 3/3] Update LenientNavHostFragment.kt --- .../src/main/kotlin/bottom_nav_watson/LenientNavHostFragment.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bottom-nav-watson/src/main/kotlin/bottom_nav_watson/LenientNavHostFragment.kt b/bottom-nav-watson/src/main/kotlin/bottom_nav_watson/LenientNavHostFragment.kt index 613ad63..2711af2 100644 --- a/bottom-nav-watson/src/main/kotlin/bottom_nav_watson/LenientNavHostFragment.kt +++ b/bottom-nav-watson/src/main/kotlin/bottom_nav_watson/LenientNavHostFragment.kt @@ -15,7 +15,7 @@ class LenientNavHostFragment : NavHostFragment() { try { super.onDestroyView() } catch (e: IllegalStateException) { - // We only sallow the exception when it contains the message related with the issue that we know. + // We only swallow the exception when it contains the message related with the issue that we know. // Otherwise we rethrow, this is brittle as it ties the implementation to a private reporting message error, // but we prefer this way to avoid hiding unrelated exceptions. if (e.message?.contains("does not have a NavController set") == false) {