title: Navigation.findNavController() in Fragment does not work properly
body: <p>I'm trying develope single-activity application using the navigation to corresponding fragment from my home fragment under certain conditions (see my code fragment below). But when I start application with the condition <code>isLoggedIn == true</code>, an exception appears:</p>
  <pre><code>2022-05-01 17:42:22.888 14974-14974/com.example.shercofaqapp E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.example.shercofaqapp, PID: 14974
  java.lang.IllegalStateException: Fragment LoginFragment{d69320f} (2bebfc01-ce4e-435e-be4f-821b5d142eb8 id=0x7f0a0096) did not return a View from onCreateView() or this was called before onCreateView().
      at androidx.fragment.app.Fragment.requireView(Fragment.java:2008)
      at com.example.shercofaqapp.view.LoginFragment.onCreateView(LoginFragment.kt:47)
      at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2995)
      at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:523)
      at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261)
      at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:113)
      at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1374)
      at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2841)
      at androidx.fragment.app.FragmentManager.dispatchViewCreated(FragmentManager.java:2777)
      at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:3020)
      at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:551)
      at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261)
      at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:113)
      at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1374)
      at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2841)
      at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2784)
      at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:262)
      at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:478)
      at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246)
      at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1425)
      at android.app.Activity.performStart(Activity.java:7825)
      at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3294)
      at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
      at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
      at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
      at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
      at android.os.Handler.dispatchMessage(Handler.java:107)
      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)
  </code></pre>
  <p>This is a fragment of my code:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>package com.example.shercofaqapp.view
  
  import android.content.Context
  import android.os.Bundle
  import android.text.TextUtils
  import androidx.fragment.app.Fragment
  import android.view.LayoutInflater
  import android.view.View
  import android.view.ViewGroup
  import android.widget.Toast
  import androidx.databinding.DataBindingUtil
  import androidx.navigation.Navigation
  import com.example.shercofaqapp.R
  import com.example.shercofaqapp.databinding.FragmentLoginBinding
  import com.google.firebase.auth.FirebaseAuth
  import com.google.firebase.auth.FirebaseUser
  import kotlin.properties.Delegates
  
  class LoginFragment : Fragment() {
  
      lateinit var binding: FragmentLoginBinding
      private var isLoggedIn by Delegates.notNull&lt;Boolean&gt;()
  
      override fun onCreateView(
          inflater: LayoutInflater, container: ViewGroup?,
          savedInstanceState: Bundle?
      ): View? {
  
          val sharedPref = requireActivity()
              .getSharedPreferences("MyPreferences", Context.MODE_PRIVATE)
          val editor = sharedPref.edit()
          var isLoggingIn = false
  
  
          binding = DataBindingUtil.inflate(
              inflater, R.layout.fragment_login, container, false)
  
          binding.apply {
              //Log in / sign up logic
              isLoggedIn = sharedPref.getBoolean("isLoggedIn", false)
              if (isLoggedIn) {
                  Navigation.findNavController(requireView())
                      .navigate(R.id.action_loginFragment_to_garageFragment)
              }
          }
          return binding.root
      }
  }</code></pre>
  </div>
  </div>
  </p>
  <p>The question is: how can I get <code>view</code> for Navigation before <code>onCreateView()</code> return it?</p>
  <p>I tried to use <a href="https://stackoverflow.com/questions/60681611/why-doesnt-requireview-not-return-a-view-onresume">this</a> solution, but I don't understand what argument I should pass to <code>.findNavController(...)</code> in my case.</p>
  <p>P.S. NavHostFragment is in MainActivity:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;layout
      xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      tools:context=&quot;.MainActivity&quot;&gt;
  
      &lt;androidx.fragment.app.FragmentContainerView
          android:id=&quot;@+id/containerView&quot;
          android:name=&quot;androidx.navigation.fragment.NavHostFragment&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot;
          app:defaultNavHost=&quot;true&quot;
          app:navGraph=&quot;@navigation/nav_graph&quot; /&gt;
  
  &lt;/layout&gt;
  </code></pre>