问题描述
Kotlin/Android 新手。请耐心等待。
我的应用程序具有以下导航方案:带有导航控制器的底部导航视图,其中片段一、二和三是“顶级目的地”,我希望从中进一步在导航层次结构中向下导航。我在 this video tutorial 之后实施了该方案。
我的问题
片段三是一个二维码扫描仪。成功扫描后,我的目标是导航到片段 X。但是,当我到达导航点时,片段 X 已成为 currentDestination (findNavController().currentDestination
) 并且导航失败并显示错误
java.lang.IllegalArgumentException: Navigation action/destination action_fragmentThree_to_fragmentX cannot be found from the current destination
如果我改为从片段 Three 的 onCreate 导航到片段 X,则一切正常。因此,导航设置必须正确。
故障排除
我对片段生命周期知之甚少,但查看导致导航的片段的状态,我认为有些不对劲。
Fragment Three -> onResume()
// QR-CODE is scanned and QR-string is captured (`ImageAnalysis.Analyzer`); method in fragment Three gets executed
// Current destination = fragment Three
// Method in fragment Three validates the QR-string using some simple String extension methods.
Fragment X -> onCreate()
Fragment X -> onCreateView()
// Current destination = fragment X
Fragment Three -> onPause()
Fragment Three -> onStop()
// Navigation fails
最有可能导致片段 X 被创建的原因是什么? 这是使它成为当前目的地的原因吗? 如果是这样,我该如何处理?强>
任何建议将不胜感激。如果您需要查看代码的某些部分,请告诉我。
编辑 1:
导航片段 X 也是当前导航片段。 Android Navigation Architecture Component - Get current visible fragment
EDIT 2 - 片段三 (ScanFragment) 代码:
带回调的 imageAnalyzer 初始化,在片段 Threes onViewCreated
imageAnalyzer = ImageAnalyzer()
imageAnalyzer.codeWasScanned = { scannedCode ->
if(!ScanningSession().isActive) { // internal class ScanningSession : Application() {}
uponSuccessfulScan(scannedCode)
}
}
fun uponSuccessfulScan(qrString: String) {
// For debugging purposes
println("ScanFragment || Scanned QR-code: $qrString")
// Validate the code and handle it
val identifierInScannedCode = ScanningSession().getIdentifierFor(qrString)
println(identifierInScannedCode)
if (identifierInScannedCode.isValid()) {
println("ScanFragment || The QR-code is valid")
if (identifierInScannedCode.isValidVehicleIdentifier()) {
println("ScanFragment || Vehicle was scanned")
if(!vehicleScanIsdisabled) {
println("ScanFragment || Vehicle scan is enabled")
// Prepare and navigate to fragment X
println("Current destination: ${findNavController().currentDestination}")
println("Current navigation fragment: ${activity?.supportFragmentManager?.currentNavigationFragment}")
findNavController().navigate(R.id.action_scanFragment_to_fragmentX)
} else {
//Todo
}
} else if (identifierInScannedCode.isValidRoomOrPlaceIdentifier()){
println("ScanFragment || Room or place was scanned")
}
} else {
println("ScanFragment || The QR-code is NOT valid ")
//presentInvalidCodeAlert()
}
}
编辑 3 - ImageAnalyzer 代码:
class ImageAnalyzer: ImageAnalysis.Analyzer {
var codeWasScanned: ((String) -> Unit)? = null
override fun analyze(imageProxy: ImageProxy) {
scanBarcode(imageProxy)
}
@SuppressLint("UnsafeExperimentalUsageError")
private fun scanBarcode(imageProxy: ImageProxy) {
imageProxy.image?.let { image ->
val inputimage = Inputimage.fromMediaimage(image,imageProxy.imageInfo.rotationdegrees)
val scanner = BarcodeScanning.getClient()
scanner.process(inputimage)
.addOnCompleteListener {
imageProxy.close()
if (it.isSuccessful) {
readBarcodeData(it.result as List<Barcode>)
} else {
it.exception?.printstacktrace()
}
}
}
}
private fun readBarcodeData(barcodes: List<Barcode>) {
for (barcode in barcodes) {
when (barcode.valueType) {
Barcode.TYPE_TEXT -> {
if (barcode.displayValue != null) {
this.codeWasScanned?.let { it(barcode.displayValue!!) }
} else {
// Tell user that the barcode Couldn't be read properly. Or any other guidance.
}
}
}
}
}
}
编辑 4:
好的,我玩了一下导航。我没有导航到片段 X,而是写了 findNavController().navigateUp()
。这让我回到了预期的主要片段(片段 2)。但是,在成功扫描后导航到片段 X 后,下一次没有。
我猜因为我从来没有弹出片段 X 它被保存在一个活动/可见状态,这以某种方式搞砸了一切。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)