Search code examples
androidinterfaceandroid-viewpagerfragmentandroid-tabactivity

How to pass data from activity to Fragment which is inside viewpager


I have one tab activity that contain 3 tabs.

I have one edittext and button in my activity, one textview in fragments.

Whenever I need to change the fragment textview I simply add some text in edittext and click the button after. That text should appear in the fragment. Here I am not able to use setArguments.


Solution

  • If you are using ViewPager to render the fragment use this code in your parent Activity.

    if(viewPager.getCurrentItem() == 1) //First fragment
    {
         FragmentOne frag1 = (FragmentOne)viewPager.getAdapter().instantiateItem(viewPager, viewPager.getCurrentItem());
         frag1.textview.setText(yourText);
    }