title: Android : Fragment and Observer
body: <p>Main goal is to update Fragment info mainly from its own class.</p>
  
  <p><strong>Main activity:</strong></p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
      final Handler GUIHandler = new Handler();
      final Runnable r = new Runnable()
      {
  
          public void run()
          {
             updateFragments();
             GUIHandler.postDelayed(this, 1000);
          }  
      };
  
      @Override
      protected void onPause() {
          super.onPause();
          GUIHandler.removeCallbacks(r);
      }
  
      @Override
      protected void onResume() {
          super.onResume();
          GUIHandler.postDelayed(r, 600);
      }
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          ...
          mViewPager = (ViewPager) findViewById(R.id.pager);
          mPagerAdapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
          mViewPager.setAdapter(mPagerAdapter);
          ...
       }
  
      private void updateFragments() {
          mPagerAdapter.updateFragments();
      }
  </code></pre>
  
  <p><strong>PagerAdapter:</strong></p>
  
  <pre><code>public class PagerAdapter extends FragmentStatePagerAdapter {
      int mNumOfTabs;
      private Observable mObservers = new FragmentObserver();
  
      public PagerAdapter(FragmentManager fm, int NumOfTabs) {
              super(fm);
              this.mNumOfTabs = NumOfTabs;
      }
  
      @Override
      public Fragment getItem(int position) {
          mObservers.deleteObservers(); // Clear existing observers.
          switch (position) {
              case 0:
                  FragmentWeather weatherTab = new FragmentWeather();
                  weatherTab.setActivity(mActivity);
                  if(weatherTab instanceof Observer)
                      mObservers.addObserver((Observer) weatherTab);
                  return weatherTab;
              case 1:
                  FragmentMemo tab2 = new FragmentMemo();
                  return tab2;
              case 2:
                  FragmentHardware tab3 = new FragmentHardware();
                  return tab3;
              default:
                  return null;
          }
      }
  
      public void updateFragments() {
          mObservers.notifyObservers();
      }
  
  }
  </code></pre>
  
  <p><strong>FragmentObserver</strong></p>
  
  <pre><code>public class FragmentObserver extends Observable {
      @Override
      public void notifyObservers() {
          setChanged(); // Set the changed flag to true, otherwise observers won't be notified.
          super.notifyObservers();
  
          Log.d("Observer", "Sending notification");
      }
  }
  </code></pre>
  
  <p><strong>FragmentWeather:</strong></p>
  
  <pre><code>public class FragmentWeather extends Fragment implements Observer {
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
           ...
          return layout;
      }
  
      public void setTemperatures(){
          Log.d("Android", "setTemperatures is called");
      }
  
      @Override
      public void update(Observable observable, Object data) {
          setTemperatures();
      }
  }
  </code></pre>
  
  <p>Problem now is, that <code>PagerAdapter::getItem()</code> doesnt get called when Fragments are created at the start of application. That means <code>WeatherFragment</code> dont get associated with <code>mObservers</code>. If I swipe to the 3rd view and then swipe back, everything is working properly. How to restructurize this to make it working?</p>