Jay
Jay

Reputation: 5084

Unable to call Fragment's Method from Parent View

I am trying to call a Fragment's public Method from its Parent View and I followed this, this and these questions on Stack. However the method I need is not avilable when I try to call it from the Parent view.

Here's my code:

Fragment:

public class FirstTabView extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.activity_first_tab_view, container, false);
            return rootView;
    }

     //The method I need to call
     public void PostStuff(String caption){
     }
}

Parent View: (Which contains the Main View Pager)

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_view);

        pager = (ViewPager) findViewById(R.id.mainpager);
        mAdapter = new HomePagerAdapter(getSupportFragmentManager());
        pager.setAdapter(mAdapter);
        pager.setOffscreenPageLimit(4);

        //Getting the Fragment's Tag. o is the Pager's Fragment index. i.e.: The first fragment
        String fragment_tag = "android:switcher:" + pager.getId() + ":" + 0;
        FirstTabView fragment = (FirstTabView) getSupportFragmentManager().findFragmentByTag(fragment_tag);
        fragment.  <This is where I need to call the method and pass the parameter>   ();
        //The method however doesn't appear when I try to reference it
}

Am I calling the Fragment's method correctly from my Parent View? What am I doing wrong?

EDIT

This is the adapter that I used to push the Fragments:

public class HomePagerAdapter extends FragmentPagerAdapter {

        public HomePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int index) {

            switch (index) {
                case 0:
                    return new FirstTabView();
                case 1:
                    return new SecondTabView();
                case 2:
                    return new ThirdTabView();
                case 3:
                    return new FourthTabView();
                case 4:
                    return new FifthTabView();
            }

            return null;
        }

        @Override
        public int getCount() {
            // get item count - equal to number of tabs
            return 5;
        }

    }

Upvotes: 2

Views: 87

Answers (1)

Shiba Prasad J.
Shiba Prasad J.

Reputation: 387

You can save a reference of the FirstTabView to your MainActivity, then call the method of the fragment.

FirstTabView

public class FirstTabView extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.activity_first_tab_view, container, false);
            return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        //set this fragment to your MainActivity
        MainActivity mainActivity = (MainActivity)getActivity();
        mainActivity.setFirstTabView(this);
    }


     //The method I need to call
     public void PostStuff(String caption){

     }
}

MainActivity

private FirstTabView firstTabView;
@Override
public void onCreate(Bundle bundle) {
    ........
    if(firstTabView != null) {
        firstTabView.PostStuff("Hello World");
    }
}

public void setFirstTabView(FirstTabView firstTabView){
    this.firstTabView = firstTabView
}

Upvotes: 2

Related Questions