title: Keeping a reference to a View in a Fragment causes memory leaks?
body: <p><strong>Somebody told me the following, but I am a bit perplexed.</strong></p>
  
  <p><strong>Please, would you be able to confirm or dispute it?</strong></p>
  
  <p>(the Fragment is not retained via <code>setRetainInstance()</code></p>
  
  <hr>
  
  <p>At the moment it is a common practice to initialize views in Fragments like this:</p>
  
  <pre><code>private lateinit var myTextView: TextView
  
  fun onViewCreated(view: View, bundle: Bundle) {
  
       ...
  
       myTextView = view.findViewById(R.id.myTextViewId)
  
       ...
  
  }
  </code></pre>
  
  <p>And then we never nullify this property. Though this is a common practise, it is causing a memory leak.</p>
  
  <p>Background to this:</p>
  
  <p>Let's say, <code>FragmentA</code> has a reference to a childView of it's <code>View</code>, as an instance field.
  Navigation from fragment A to B is executed by FragmentManager using a specific FragmentTransaction. Depending on the type of transaction, the Manager might want to kill the <code>View</code> only but still persist the instance of <code>FragmentA</code>(see below lifecycle part where it says "The fragment returns to the layout from the back stack"). When user navigates back from <code>FragmentB</code> to <code>FragmentA</code>, the previous instance of <code>FragmentA</code> will be brought to the front, but a new <code>View</code> will be created.</p>
  
  <p>The issue is that if we keep instance to our view in the lateinit property and never clear the reference to it, the view cannot be fully destroyed, causing memory leak.</p>