0

In my application I have one method in a Fragment class. I want to call this method using the parent Activity.

Fragment code :

public class SampleFragment extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_sample, null);

        return v;
    }

    public void getToast(){
        Toast.makeText(getActivity(), "Test", Toast.LENGTH_SHORT).show();
    }
}

And I want use this codes to initialize fragment in Activity :

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFrag(new SampleFragment (), context.getResources().getString(R.string.info));
    viewPager.setOffscreenPageLimit(1);
    viewPager.setAdapter(adapter);
}

private static class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFrag(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

How can I call getToast() method in Activity?

I am amateur, please help me with my above codes? Please my friends. Thanks

  • 1
    get the reference of fragment then call the method – Shubham Jain Sep 27 '17 at 08:29
  • Just use `new SampleFragment().getToast();` in your `Activity` code . – KeLiuyue Sep 27 '17 at 08:30
  • @ShubhamJain, hey my friend I'm really amateur and need your help. please send to me code with my above codes too see and I learn it. please –  Sep 27 '17 at 08:31
  • @KeLiuyue, thanks my friend. your code work for me. but this is good way? not bad way? thanks my bro –  Sep 27 '17 at 08:36

5 Answers5

1

The simplest way is to store locally in your activity reference to your fragment and call getToast on it:

SampleFragment myFragment; // declared in your activity class

// This is small change in your setupViewPager
myFragment = new SampleFragment ();
adapter.addFrag(myFragment, context.getResources().getString(R.string.info));

//later on when you want to call getToast:
myFragment.getToast();

You may also skip storing SampleFragment as a data member of your activity and retrievie its reference directly from FragmentManager when its needed.

marcinj
  • 48,511
  • 9
  • 79
  • 100
0

Or if you dont wish to store an instance locally,

you can try this where ever you need to call the getToast method:

if (viewpagerAdapter.getItem(viewpager.getCurrentItem()) instanceof SampleFragment){
((SampleFragment)viewpagerAdapter.getItem(viewpager.getCurrentItem())).getToast();
}
MadScientist
  • 2,134
  • 14
  • 27
0

You can create object of Fragment and call that getToast() method. Apart from this I have one suggestion, Use Common class which have method for showing Toast.

public class CommonUtil {
    /**
     * This function will show toast in the application
     * @param message
     */
    public static void showToast(String message) {
        if (!message.isEmpty()) {
            Toast.makeText(MyApplication.getContext(), message, Toast.LENGTH_SHORT).show();
        }
    }
}

With the use of this you can call this method anywhere from the project.

Kuls
  • 2,047
  • 21
  • 39
0

To call a method, you need object of that fragment

Using following you can get it

  SampleFragment frag = (SampleFragment ) getSupportFragmentManager()
                              .findFragmentById(R.id.fragment_container);
                          // or  getFragmentManager() based on your imports

   frag.getToast();
0

You can use :

//in place of viewPager.getcurrentItem() you can also pass direct int position of your fragment 
Fragment frag=adapter.getItem(viewPager.getcurrentItem()); //viewpagerAdapter adapter declared globally

if(frag instance of MyFragment)
{
 MyFragment myFragment = (MyFragment) frag;
 frag.mymethod(); //call to your method showToast
}

Hope it helps

N.Moudgil
  • 709
  • 5
  • 11