This repository has been archived by the owner on Dec 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6707dfe
commit 1cf5b35
Showing
2 changed files
with
37 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
bottom-nav-watson/src/main/kotlin/bottom_nav_watson/LenientNavHostFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) } | ||
} | ||
} | ||
|
||
} |