title: ViewPager Fragments are not initialized immediately
body: <p>I have a <code>ViewPager</code> with an adapter of <code>FragmentStatePagerAdapter</code>. For the sake of simplicity I have 2 Fragments within my ViewPager. </p>
  
  <pre><code>class MyPagerAdapter(fragmentManager: FragmentManager): FragmentStatePagerAdapter(fragmentManager) {
      var firstFragment: SampleFragment? = null
      var secondFragment: SampleFragment? = null
  
      override fun getItem(position: Int): Fragment {
          return when(position) {
              0 -&gt; SampleFragment()
                  .also { firstFragment = it }
              1 -&gt; SampleFragment()
                  .also { secondFragment = it }
              else -&gt; Fragment()
          }
      }
  
      override fun getCount(): Int {
          return 2
      }
  
  }
  </code></pre>
  
  <p>My <code>SampleFragment</code> is a simple Fragment with a <code>TextView</code> on it's view. </p>
  
  <pre><code>class SampleFragment: Fragment() {
      override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
          return inflater.inflate(R.layout.fragment_sample, container, false)
      }
  
      fun setText(text: String) {
          text_view.text = text
      }
  }
  </code></pre>
  
  <p>Now I am on my MainActivity where I have ViewPager, I have below lines</p>
  
  <pre><code>val adapter = MyPagerAdapter(supportFragmentManager)
  view_pager.adapter = adapter
  
  adapter.firstFragment?.setText("Some Text")
  </code></pre>
  
  <p>So, here is a problem. After initializing (setting adapter) my ViewPager I expect my fragments are created. But it is not the case. My fragments are initialized after a certain delay. So when I want to set text of TextView inside my fragment with the line</p>
  
  <pre><code>adapter.firstFragment?.setText("Some Text")
  </code></pre>
  
  <p><code>firstFragment</code> is null. </p>
  
  <p>When I debug, I see that after</p>
  
  <pre><code>view_pager.adapter = adapter
  </code></pre>
  
  <p><code>getCount()</code> of adapter is getting called. And then 
  adapter.firstFragment?.setText("Some Text")
  will be executed which doesn't have any effect as <code>firstFragment</code> is <code>null</code>. And after a second or so <code>getItem()</code> of adapter is getting called.</p>
  
  <p>So my question is why is my viewpager adapter fragments aren't getting initialized immediately. If I am missing something could you point out. Or it is expected behavior of ViewPager Adapter then how can I make viewpager fragments get initialized whenever pager is initialized? </p>