title: How to update fragment content from activity (viewpager)?
body: <p>I have a simple viewPager with two fragments. Each fragment contains a TextView. I would like to be able to update the text of the current textView displayed from the activity.</p>
  
  <p><strong>Activity :</strong></p>
  
  <pre><code>public class MainActivity extends FragmentActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main_activity);
  
          ArrayList&lt;Fragment&gt; fragments = new ArrayList&lt;Fragment&gt;();
          fragments.add(Fragment.instantiate(this, Live.class.getName()));
          fragments.add(Fragment.instantiate(this, Hit.class.getName()));
  
          this.pagerAdapter = new MyPagerAdapter(getSupportFragmentManager(),fragments);
  
          ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
          pager.setAdapter(pagerAdapter);
      }
  }
  </code></pre>
  
  <p><strong>Adapter :</strong></p>
  
  <pre><code>public class MyPagerAdapter extends FragmentPagerAdapter {
  
      private final ArrayList&lt;Fragment&gt; fragments;
  
      public MyPagerAdapter(FragmentManager fm, ArrayList&lt;Fragment&gt; fragments)
      {
          super(fm);
          this.fragments = fragments;
      }
  
      @Override
      public Fragment getItem(int position) {
          return this.fragments.get(position);
      }
  
      @Override
      public int getCount() {
          return this.fragments.size();
      }
  }
  </code></pre>
  
  <p><strong>Fragment :</strong></p>
  
  <pre><code>public class Live extends Fragment {
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          return inflater.inflate(R.layout.live, container,false);
      }
  }
  </code></pre>
  
  <p><strong>XML Fragment :</strong></p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"&gt;
      &lt;TextView android:layout_height="fill_parent"
                android:layout_width="fill_parent"
                android:text="First Fragment"
                android:id="@+id/status"/&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Each fragment have a TextView with the id "status".
  Updating the text view in <code>onCreateView</code> works. But how can i update the textView from anywhere in the activity ?</p>
  
  <p>I have tried this without succes : </p>
  
  <p><strong>Fragment :</strong> </p>
  
  <pre><code>public class Live extends Fragment {    
      public TextView tv;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.live, container,false);
          tv = (TextView) view.findViewById(R.id.status);
  
          return view;
      }
  }
  </code></pre>
  
  <p><strong>Activity :</strong></p>
  
  <pre><code>@Override
      protected void onResume() {
          super.onResume();
          Live frag = (Live) pagerAdapter.getItem(0);
          frag.tv.setText("Test 2");
      }
  </code></pre>
  
  <p>But it gives me a nullPointerException on <code>tv</code>, probably because the fragment is not yet inflated.</p>
  
  <p>I already read the following questions without success : </p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/12341591/updating-fragments-from-activity-in-a-viewpager">Updating fragments from Activity in a ViewPager</a>  </li>
  <li><a href="https://stackoverflow.com/questions/9151811/update-a-textview-in-a-viewpager-from-another-fragment">Update a TextView in a ViewPager from another Fragment</a></li>
  </ul>