title: Fragment doesn't get value from ViewModel
body: <p>I have an single activity application with jetpack navigation, I need an object variable for all my application in many fragments. So I use a ViewModel, and I've created a Parent Fragment class which provide the ViewModel :</p>
  
  <pre class="lang-kotlin prettyprint-override"><code>class MyViewModel : ViewModel() {
      var myData : CustomClass? = null
      ...
  }
  
  open class ParentFragment : Fragment {
      val model : MyViewModel by activityViewModels()
      lateinit var myData : CustomClass
  
      override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
          super.onViewCreated(view, savedInstanceState)
  
          model.myData?.let {
              myData = it
          }
      }
  }
  </code></pre>
  
  <p><code>myData</code>should not be null where I use <code>ParentFragment</code>, but sometimes, randomly I get   <code>kotlin.UninitializedPropertyAccessException: lateinit property myData has not been initialized</code> when I use <code>myData</code></p>
  
  <p>Is it possible that my ViewModel doesn't keep <code>myData</code>? How can I be sure that my property has been initialized ?</p>
  
  <p><strong>UPDATE : Try 1</strong></p>
  
  <p>I've tried this code in my <code>ParentFragment</code>:</p>
  
  <pre class="lang-kotlin prettyprint-override"><code>open class ParentFragment : Fragment {
      val model : MyViewModel by activityViewModels()
      lateinit var backingData : CustomClass
      val myData : CustomClass
          get() {
              if (!::backingData.isInitialized)
                  model.getData()?.let {
                      backingData = it
                  }
              return backingData
          }
  
      override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
          super.onViewCreated(view, savedInstanceState)
  
          model.getData?.let {
              backingData = it
          }
      }
  }
  </code></pre>
  
  <p>But the problem doesn't disappear when I call <code>myData</code>, it seem's the <code>ViewModel</code>loses my data</p>
  
  <p><strong>UPDATE 2 : More code details</strong></p>
  
  <p>Before to go inside a <code>fragment</code> which extends <code>ParentFragment</code>, I set my data in <code>ViewModel</code> and then I navigate to the next <code>fragment</code> as below :</p>
  
  <pre class="lang-kotlin prettyprint-override"><code>// Inside FirstFragment
  if (myData != null) {
      model.setData(myData)
      findNavController().navigate(FirstFragmentDirections.actionFirstToNextFragment())
  }
  </code></pre>
  
  <p>Is it possible that my <code>NavController</code> does navigation before the data was setted ?</p>
  
  <p><strong>EDIT 3 : Try to use custom Application class</strong></p>
  
  <p>According to an answer below, I've implemented a <code>custom Application class</code>, and I've tried to pass my object through this class :</p>
  
  <pre class="lang-kotlin prettyprint-override"><code>class MyApplication: Application() {
  
      companion object {
          var myObject: CustomClass? = null
      }
  }
  </code></pre>
  
  <p>But unfortunately, there is no change for me. Maybe my object is too big to allocate correctly ?</p>