导航控制器和广播接收器

时间:2018-07-13 06:52:46

标签: android broadcastreceiver navigationcontroller

我目前在从NavigationController上下文中查找BroadcastReceiver实例时遇到问题。当我从findNavigationController()中的其他任何地方调用Fragment时,它都可以正常导航,但是当我从onReceive的上下文中调用它时,应用程序崩溃,但出现以下异常

 Caused by: java.lang.IllegalArgumentException: navigation destination app.eventcloud.events:id/action_documents_list_fragment_to_document_viewer_fragment is unknown to this NavController
    at androidx.navigation.NavController.navigate(NavController.java:633)
    at androidx.navigation.NavController.navigate(NavController.java:592)
    at app.eventcloud.root.gui.screens.knowledgehub.DocumentsListFragment.handleDownloadCompleted(DocumentsListFragment.kt:97)
    at app.eventcloud.root.gui.screens.knowledgehub.DocumentsListFragment.access$handleDownloadCompleted(DocumentsListFragment.kt:26)
    at app.eventcloud.root.gui.screens.knowledgehub.DocumentsListFragment$receiver$1.onReceive(DocumentsListFragment.kt:143)
    at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0(LoadedApk.java:1391)
    at android.app.-$$Lambda$LoadedApk$ReceiverDispatcher$Args$_BumDX2UKsnxLVrE6UJsJZkotuA.run(Unknown Source:2) 
    at android.os.Handler.handleCallback(Handler.java:873) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6669) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

导致此问题的代码如下。

private val receiver: BroadcastReceiver = object : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        val action = intent.action
        if (DownloadManager.ACTION_DOWNLOAD_COMPLETE == action) {
            ...
            val filePath = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))
            val bundle = Bundle()
            bundle.putString("file_path", filePath)
            findNavController().navigate(R.id.action_documents_list_fragment_to_document_viewer_fragment, bundle)
            ...
        }
    }
}

我尝试从NavigationController之外的上下文中获取BroadcastReceiver,但仍然遇到相同的问题。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我最终要做的是在文档https://developer.android.com/topic/libraries/architecture/navigation/navigation-implementing#Deeplink中创建喜欢的深层链接

然后使用配置的Deeplink开始活动

            val deepLinkIntent = Intent(Intent.ACTION_VIEW)
            deepLinkIntent.data = Uri.parse("epix://downloads/$filePath")
            context.startActivity(deepLinkIntent)

也请https://codelabs.developers.google.com/codelabs/android-navigation/#10

查看此代码实验室部分