title: Access Parent Fragment method from Child Fragment
body: <p>I'm trying to access from <code>Map</code> Fragment to <code>myfunction()</code> situed in the Parent Fragment <code>Tabs</code>.</p>
  
  <p>I tried the following line <code>Tabs parentFragment = (Tabs) getParentFragment();</code> however parentFragment is null.</p>
  
  <p>I read all topics concerning this subjects but none of them helped me... (after maybe I misunderstood them).</p>
  
  <p>I have the following files :</p>
  
  <p>The Fragment from which I want to call the Parent method.</p>
  
  <pre><code>public class Map extends Fragment {
  
  View rootView;
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
  
      rootView = inflater.inflate(R.layout.map_fragment, container, false);
      Tabs parentFragment = (Tabs) getParentFragment();
      parentFragment.myfunction();
      // it crash because parentFragment is null...
      return rootView;
      }
  }
  </code></pre>
  
  <hr>
  
  <p>The Parent Fragment which contain the myfunction() method.</p>
  
  <pre><code>public class Tabs extends Fragment {
  private View rootview;
  
  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
  container, @Nullable Bundle savedInstanceState) {
      super.onCreateView(inflater, container, savedInstanceState);
      rootview = inflater.inflate(R.layout.tabs, container, false);
  
      TabLayout tabLayout = (TabLayout) rootview.findViewById(R.id.tab_layout);
      tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
      tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
      tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
      tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
  
      final ViewPager viewPager = (ViewPager) rootview.findViewById(R.id.pager);
      Context context = (HomeContainer)getContext();
      final TabsAdapter adapter = new TabsAdapter
              (getActivity().getSupportFragmentManager(), tabLayout.getTabCount());
      viewPager.setAdapter(adapter);
      viewPager.setOffscreenPageLimit(3);
      viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
      tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
          @Override
          public void onTabSelected(TabLayout.Tab tab) {
              viewPager.setCurrentItem(tab.getPosition());
          }
  
          @Override
          public void onTabUnselected(TabLayout.Tab tab) {
  
          }
  
          @Override
          public void onTabReselected(TabLayout.Tab tab) {
  
          }
      });
      return rootview;
  }
  
  // the function I desire to call
  public void myfunction(){
  }
  
  }
  </code></pre>
  
  <hr>
  
  <p>The <code>TabsAdapter</code> (because <code>Map</code> Fragment is a Tab).</p>
  
  <pre><code>public class TabsAdapter extends FragmentStatePagerAdapter {
  int mNumOfTabs;
  
  public TabsAdapter(FragmentManager fm, int NumOfTabs) {
      super(fm);
      this.mNumOfTabs = NumOfTabs;
  }
  
  @Override
  public Fragment getItem(int position) {
  
      switch (position) {
          case 0:
              Map tab1 = new Map();
              return tab1;
          case 1:
              Settings tab2 = new Settings();
              return tab2;
          case 2:
              Others tab3 = new Others();
              return tab3;
          default:
              return null;
      }
  }
  
  @Override
  public int getCount() {
      return mNumOfTabs;
  }
  }
  </code></pre>
  
  <p>I relatively new to Android, so I'm a bit lost !
  Thanks in advance for your help.</p>