title: Accessing a tabbed fragment method from the parent activity?
body: <p>Hi (I'm new to this so you'll have to forgive me)</p>
  
  <p>I currently have a tabbed activity where <code>MainActivity.java</code> is the parent class and <code>Absences.java</code> and <code>Notices.java</code> are the fragments. I have a date picker fragment inside the <code>MainActivity</code> that is run when I tap on an options menu button.</p>
  
  <p>When I've successfully selected my date I want to pass the information to a method in the Absences fragment. However, I'm really struggling with how to implement this part.</p>
  
  <p>The research I've done seems to suggest this is the solution:</p>
  
  <pre><code>Absences fragment= (Absences) getSupportFragmentManager().findFragmentById(R.id.Absences);
          ((Absences)fragment).updateAbsences();
  </code></pre>
  
  <p>However, <code>getSupportFragmentManager()</code> can't be referenced from a static context and I can't seem to find the id of my Fragment.</p>
  
  <p>After doing a little more research it seemed to reveal this method doesn't work for a tabbed activity so I'm at a loss at this point.</p>
  
  <p>Would someone be able to point me in the right direction? - I've been working on this for quite some time now!</p>
  
  <p><strong>SectionsPageAdapter class</strong></p>
  
  <pre><code>Fragment one, two;
  
  public class SectionsPagerAdapter extends FragmentPagerAdapter {
  
      public SectionsPagerAdapter(FragmentManager fm) {
          super(fm);
      }
  
      @Override
      public Fragment getItem(int position) {
          // getItem is called to instantiate the fragment for the given page.
          // Return a PlaceholderFragment (defined as a static inner class below).
          switch (position) {
              case 0:
                  if(one==null){
                      one = new Absences();
                  }
                  return one;
              case 1:
                  if(two==null){
                      two = new Notices();
                  }
                  return two;
          }
          return null;
      }
  
      @Override
      public int getCount() {
          // Show 3 total pages.
          return 2;
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
          switch (position) {
              case 0:
                  return "ABSENCES";
              case 1:
                  return "NOTICES";
          }
          return null;
      }
  
  }
  </code></pre>
  
  <p><strong>onOptionsItemSelected</strong></p>
  
  <pre><code> public boolean onOptionsItemSelected(MenuItem item) {
      int id = item.getItemId();
      if (id == R.id.calendar) {
          DialogFragment newFragment = new DatePickerFragment();
          newFragment.show(getSupportFragmentManager(), "datePicker");
      }
      return super.onOptionsItemSelected(item);
  }
  </code></pre>
  
  <p><strong>DatePickerFragment</strong></p>
  
  <pre><code>public static class DatePickerFragment extends DialogFragment
          implements DatePickerDialog.OnDateSetListener {
  
      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState) {
          final Calendar c = Calendar.getInstance();
          int year = c.get(Calendar.YEAR);
          int month = c.get(Calendar.MONTH);
          int day = c.get(Calendar.DAY_OF_MONTH);
          DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
          dialog.getDatePicker().setMaxDate(c.getTimeInMillis());
          dialog.setTitle("");
          return  dialog;
      }
  
      public void onDateSet(DatePicker view, int year, int month, int day) {
          Absences fragment= (Absences) getSupportFragmentManager().findFragmentById(R.id.Absences);
          ((Absences)fragment).updateAbsences();
      }
  }
  </code></pre>
  
  <p><strong>--EDIT--</strong></p>
  
  <p>Updated code as per suggestion below</p>
  
  <p><strong>Method inside MainActivity</strong></p>
  
  <pre><code>public void onDatePicked() {
      Absences fragment = (Absences) getSupportFragmentManager().findFragmentById(R.id.absences);
      fragment.updateAbsences();
  }
  </code></pre>
  
  <p><strong>New DatePickerFragment Class</strong></p>
  
  <pre><code>    package com.alexwoohouse.heartofengland;
  
  import android.annotation.TargetApi;
  import android.app.DatePickerDialog;
  import android.app.Dialog;
  import android.icu.util.Calendar;
  import android.os.Build;
  import android.os.Bundle;
  import android.support.v4.app.DialogFragment;
  import android.util.Log;
  import android.widget.DatePicker;
  
  import java.util.Date;
  
  public class DatePickerFragment extends DialogFragment
          implements DatePickerDialog.OnDateSetListener {
  
      public interface OnDatePickedListener {
          void onDatePicked(); //This method seems to be greyed out!
      }
  
      @TargetApi(Build.VERSION_CODES.N)
      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState) {
          final Calendar c = Calendar.getInstance();
          int year = c.get(Calendar.YEAR);
          int month = c.get(Calendar.MONTH);
          int day = c.get(Calendar.DAY_OF_MONTH);
          DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
          dialog.getDatePicker().setMaxDate(c.getTimeInMillis());
          dialog.setTitle("");
          return  dialog;
      }
  
      @TargetApi(Build.VERSION_CODES.N)
      public void onDateSet(DatePicker view, int year, int month, int day) {
          Calendar cal = Calendar.getInstance();
          cal.set(Calendar.YEAR, year);
          cal.set(Calendar.MONTH, month);
          cal.set(Calendar.DAY_OF_MONTH, day);
      }
  }
  </code></pre>