title: Kotlin Android extensions and retained fragment
body: <p>I am using <a href="http://kotlinlang.org/docs/tutorials/android-plugin.html">Kotlin Android extensions</a> in my project and I came across some behavior that I am unable to understand. I use this code to retain my fragment in the activity:</p>
  
  <pre><code>val fragment = fragmentManager.findFragmentByTag("hello") ?: HelloFragment()
  fragmentManager.beginTransaction()
                 .replace(R.id.fragment_container, fragment, "hello")
                 .commit()
  </code></pre>
  
  <p>This is the retained <code>Fragment</code>:</p>
  
  <pre><code>import kotlinx.android.synthetic.hello.*
  
  public class HelloFragment : Fragment() {
      val text = "Hello world!"
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super&lt;Fragment&gt;.onCreate(savedInstanceState)
          setRetainInstance(true)
      }
  
      override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
          return inflater?.inflate(R.layout.hello, container, false)
      }
  
      override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
          super&lt;Fragment&gt;.onViewCreated(view, savedInstanceState)
  
          text_view.setText(text) // &lt;- does not work when retained
      }
  }
  </code></pre>
  
  <p>and its XML layout <strong>hello.xml</strong>:</p>
  
  <pre><code>&lt;TextView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/text_view"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:gravity="center" /&gt;
  </code></pre>
  
  <p>Everything works as expected – the <code>text_view.setText()</code> displays <em>Hello world!</em> on screen on the first launch. But when you rotate the screen the <code>text_view.setText()</code> does not work. This is weird because <code>text_view</code> is not nullable and has to reffer to some view. If you remove <code>setRetainInstance(true)</code> and leave the fragment recreate every time this problem disappears. Any thoughts what might cause this problem?</p>