Baudev
Baudev

Reputation: 141

Access Parent Fragment method from Child Fragment

I'm trying to access from Map Fragment to myfunction() situed in the Parent Fragment Tabs.

I tried the following line Tabs parentFragment = (Tabs) getParentFragment(); however parentFragment is null.

I read all topics concerning this subjects but none of them helped me... (after maybe I misunderstood them).

I have the following files :

The Fragment from which I want to call the Parent method.

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;
    }
}

The Parent Fragment which contain the myfunction() method.

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(){
}

}

The TabsAdapter (because Map Fragment is a Tab).

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;
}
}

I relatively new to Android, so I'm a bit lost ! Thanks in advance for your help.

Upvotes: 3

Views: 8362

Answers (2)

Ankit Aman
Ankit Aman

Reputation: 1009

I would recommend you to put is after onCreateView and also check for the instance.

 Fragment parentFragment = getParentFragment();

    if(parentFragment instanceof Tab) {
        ((Tab) parentFragment).myfunction();
    } 

Upvotes: 3

natario
natario

Reputation: 25204

onCreateView might be too early, try doing that onViewCreated, or onStart in the worst case.

Upvotes: 2

Related Questions