0

I want to navigate to Profile Fragment, when the user wants to sign up. The LoginBottomSheetFragment is responsible to perform this navigation whenever it pops up from the bottom and user clicks on the button. The problem is that whenever I am trying to use findNavController I get the error:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.fjklabs.pkfinance, PID: 8711
    java.lang.IllegalStateException: View android.widget.LinearLayout{32bc1d5 V.E...... ......ID 0,0-1328,935 #7f090188 app:id/rootLayout} does not have a NavController set
        at androidx.navigation.Navigation.findNavController(Navigation.java:84)
        at androidx.navigation.fragment.NavHostFragment.findNavController(NavHostFragment.java:118)
        at androidx.navigation.fragment.FragmentKt.findNavController(Fragment.kt:29)
        at com.fjklabs.pkfinance.fragment.LoginBottomDialogFragment$onCreateView$1.onClick(LoginBottomDialogFragment.kt:42)
        at android.view.View.performClick(View.java:7125)
        at android.view.View.performClickInternal(View.java:7102)
        at android.view.View.access$3500(View.java:801)
        at android.view.View$PerformClick.run(View.java:27336)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

This is the current flow:

MainActivity -> Activity A -> Open BottomSheetFragment -> Navigate to Profile Fragment

This is what (some part of) my navigation xml looks like:

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    app:startDestination="@id/home">
    <fragment
        android:id="@+id/login_bottom_sheet"
        android:name="LoginBottomDialogFragment"
        tools:layout="@layout/fragment_login_bottom_dialog">
        <action
            android:id="@+id/navigate_to_profile"
            app:destination="@id/profile"/>
    </fragment>

    <fragment
        android:id="@+id/profile"
        android:name="ProfileFragment"
        android:label="Account info"
        tools:layout="@layout/fragment_profile"/>
</navigation>

BottomSheetDialogFragment:

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val rootView = inflater.inflate(R.layout.fragment_login_bottom_dialog, container, false)
        btnLogin = rootView.findViewById(R.id.signup_user)
        btnLogin.setOnClickListener {
            dismiss()
            findNavController().navigate(LoginBottomDialogFragmentDirections.navigateToProfile())
        }
        root = rootView
        return rootView
    }

Activity A opens BottomSheetFragment:

fun showBottomDialogFragment() {
        bottomDialogFragment.show(supportFragmentManager, "LOGIN_DIALOG_FRAGMENT")
    }

Since the navigation is part of the MainActivity layout, The problem seems like Activity A does not have any access to NavController. How would get hold of NavController in such scenario?

faizanjehangir
  • 2,771
  • 6
  • 45
  • 83
  • How are you opening the `BottomSheetFragment`? Where is your `` destination? – ianhanniballake Aug 02 '21 at 02:42
  • @ianhanniballake added details for opening `BottomSheetFragment`. I have an action defined on the `BottomSheetFragment` in the `navigation`, if thats what you meant by `` destination? – faizanjehangir Aug 02 '21 at 12:43

0 Answers0