Reputation: 399
I create three fragment for swipe view on MainActivity
. That's why I use FragmentPagerAdapter
to add those fragments on MainActivity
. It's all working well. Now I create the bellow method on fragment one.
public void myToast(String text){
Toast.makeText(getActivity(), text, Toast.LENGTH_LONG).show();
}
How I can call/invoke this method from MainActivity
?
Note : I can't use findFragmentById()
or findFragmentByTag()
method because There are no id
and tag
for those fragments.
Upvotes: 1
Views: 47
Reputation: 3325
you can use these methods -
private static String makeFragmentName(int viewPagerId, int index) {
return "android:switcher:" + viewPagerId + ":" + index;
}
And to access -
YourFragment yourfragment = (YourFragment) getSupportFragmentManager().findFragmentByTag(makeFragmentName(yourpager.getId(),the fragment number must be int /* 0, 1, 2 *\));
then you can use the method of your fragment in activity-
yourfragment.maketoast()
Upvotes: 1