title: Issue when getting reference to fragment inside ViewPager from activity
body: <p>I need to set a text value to <code>EditText</code> inside Fragment. There are 5 such fragments added to <code>ViewPager</code> in my activity. In order to do this I need to get reference to the fragments inside <code>ViewPager</code>. I used below <code>ViewPager</code> adapter.</p>
  
  <pre><code>public class MessageViewPageAdapter extends FragmentPagerAdapter {
  
      private List&lt;Fragment&gt; pages;
      private HashMap&lt;Integer,Fragment&gt; fragmentReference;
  
      public MessageViewPageAdapter(FragmentManager fm, List&lt;Fragment&gt; pages) {
          super(fm);
          this.pages = pages;
          fragmentReference = new HashMap&lt;&gt;();
      }
  
      @Override
      public Fragment getItem(int position) {
          Fragment fragment = pages.get(position);
          fragmentReference.put(position,fragment);
          return pages.get(position);
      }
  
      @Override
      public int getCount() {
          return pages.size();
      }
  
      @Override
      public void destroyItem(ViewGroup container, int position, Object object) {
          super.destroyItem(container, position, object);
          fragmentReference.remove(position);
      }
  
      public Fragment getFragment(int key) {
          return fragmentReference.get(key);
      }
  }
  </code></pre>
  
  <p>As you can see fragmentReference HashMap uses to store all fragments inside <code>ViewPager</code>. <code>getFragment(int key)</code> method uses to get fragment from that hash map. This is how my activity looks like.</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      private ViewPager messageViewPage;
      private Fragment message1;
      private Fragment message2;
      private Fragment message3;
      private Fragment message4;
      private Fragment message5;
  
      private List&lt;Fragment&gt; fragments = new ArrayList&lt;&gt;();
      private MessageViewPageAdapter messageViewPageAdapter;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          messageViewPage = (ViewPager) findViewById(R.id.message_viewPage);
  
          message1 = new Message1();
          message2 = new Message2();
          message3 = new Message3();
          message4 = new Message4();
          message5 = new Message5();
          fragments.add(message1);
          fragments.add(message2);
          fragments.add(message3);
          fragments.add(message4);
          fragments.add(message5);
  
          messageViewPageAdapter = new MessageViewPageAdapter(getSupportFragmentManager(), fragments);
          messageViewPage.setAdapter(messageViewPageAdapter);
          messageViewPage.setOffscreenPageLimit(5);
  
          Message1 message1fragment = (Message1) messageViewPageAdapter.getFragment(0);
          Message2 message2fragment = (Message2) messageViewPageAdapter.getFragment(1);
          Message3 message3fragment = (Message3) messageViewPageAdapter.getFragment(2);
          Message4 message4fragment = (Message4) messageViewPageAdapter.getFragment(3);
          Message5 message5fragment = (Message5) messageViewPageAdapter.getFragment(4);
  
  
      } 
  </code></pre>
  
  <p>But i'm getting null values for all 5 fragments <code>(message1fragment,message2fragment,message3fragment,message4fragment,message5fragment)</code></p>
  
  <p>But when I put this same code inside button on click like below, it's works fine.</p>
  
  <pre><code>button.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Message1 message1fragment = (Message1) messageViewPageAdapter.getFragment(0);
                  Message2 message2fragment = (Message2) messageViewPageAdapter.getFragment(1);
                  Message3 message3fragment = (Message3) messageViewPageAdapter.getFragment(2);
                  Message4 message4fragment = (Message4) messageViewPageAdapter.getFragment(3);
                  Message5 message5fragment = (Message5) messageViewPageAdapter.getFragment(4);
              }
          });
  </code></pre>
  
  <p>What am i doing wrong ? I even try placing same code inside <code>onStart()</code> and <code>onResume()</code> methods, But same result. Every fragment inside this <code>ViewPager</code> has a method named <code>setMessage(String message)</code> which i intend to use to set text to <code>EditText</code> inside that fragment. I need to do that when activity start.</p>
  
  <p>This is what my message fragment looks like. </p>
  
  <pre><code>public class Message1 extends Fragment {
  
      private EditText message1;
  
      public Message1() {
          // Required empty public constructor
      }
  
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          View inflate = inflater.inflate(R.layout.fragment_message1, container, false);
          message1 = (EditText) inflate.findViewById(R.id.message_1);
          return inflate;
      }
  
      public String getMessageOneText(){
          return String.valueOf(message1.getText());
      }
  
      public void setErrorMessage(){
          message1.setError("Message 1 can not be empty");
      }
  
      public void setMessage(String message){
          message1.setText(message);
      }
  </code></pre>