1

I have got aplication with three screens(3 fragments and viewPager), and now I want to send for example String from Activity to FragmentA but I don't know how.

This is my Activity class with pager Adapter

public class MainActivity extends FragmentActivity {
ViewPager viewPager;


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


    viewPager= (ViewPager) findViewById(R.id.pager);
    viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));

}

}

class MyAdapter extends FragmentPagerAdapter{

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

@Override
public Fragment getItem(int arg0) {

    Fragment fragment=null;
    if(arg0==0){
        fragment=new FragmentA();


    }
    if(arg0==1){
        fragment=new FragmentB();


    }
    if(arg0==2){
        fragment=new FragmentC();


    }
    return fragment;
}

@Override
public int getCount() {
    return 3;
}
adamek339
  • 79
  • 1
  • 8
  • 1
    You could use the solution in the answer here: http://stackoverflow.com/questions/36503779/refresh-data-in-viewpager-fragment – Daniel Nugent Nov 15 '16 at 02:44

1 Answers1

-1

You can use the EventBus library or write something similar yourself, but the best option is to learn the correct and accepted methods of communication between components.

If you want to pass the data (String in your case) before the ViewPager initialized you can pass it via argumnents, here you can find a good example.

Community
  • 1
  • 1
Alex Kamenkov
  • 891
  • 1
  • 6
  • 16