5

I'm currently having an issue with finding the NavigationController instance from within a BroadcastReceiver context. When I call findNavigationController() from anywhere else in the Fragment it navigates without any problem but when I call it from within the context of onReceive the app crashes with the following exception

 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) 

The code causing the issue is the following.

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)
            ...
        }
    }
}

I've tried getting the NavigationController from a context outside of the BroadcastReceiver I still have the same issue. Any thoughts?

the-ginger-geek
  • 7,041
  • 4
  • 27
  • 45

1 Answers1

0

What I've ended up doing is create deeplinks likke in the docs https://developer.android.com/topic/libraries/architecture/navigation/navigation-implementing#Deeplink

And then starting the activity using the configured deeplink

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

Check out this codelab part also https://codelabs.developers.google.com/codelabs/android-navigation/#10

Calin
  • 6,661
  • 7
  • 49
  • 80