2

I developed an android applicaion with acion bar that has 3 tabs with 3 difrrent fragments and I tried to pass a parameter from parent activity to fragments, I used the solution given in THIS qusetion and every thing is right, the only problem is that, now the activity sends this parameter to fragments just once at the start f applicaion, I need to get this parameter each time the user select the tabs, I think I should do this opration on onTabSelected method but have no idea how to do that, any suggestion?

Community
  • 1
  • 1
user3427977
  • 115
  • 1
  • 1
  • 8

2 Answers2

0

Try this !!

    ActionBar.TabListener tabListener = new ActionBar.TabListener() {

                    @Override
                    public void onTabSelected(Tab tab, FragmentTransaction ft) {
                        // Pass the position on tab click to ViewPager
                        mPager.setCurrentItem(tab.getPosition());
                        Log.d("tab selected",""+tab.getPosition());
                                    if(tab.getPosition==0)
                                 {
                                     Fragment fragA=new FrgmentA();
                                     fragA.getData("data");
                                 }
                    }

                    @Override
                    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
                        // TODO Auto-generated method stub
                        Log.d("tab Unselected",""+tab.getPosition());
                    }

                    @Override
                    public void onTabReselected(Tab tab, FragmentTransaction ft) {
                        // TODO Auto-generated method stub
                        Log.d("tab Reselected",""+tab.getPosition());
                    }
                };

                // Create first Tab
                tab = mActionBar.newTab().setText("Profile").setTabListener(tabListener);
                mActionBar.addTab(tab);

                // Create second Tab
                tab = mActionBar.newTab().setText("Picture").setTabListener(tabListener);
                mActionBar.addTab(tab);

                // Create third Tab
                tab = mActionBar.newTab().setText("Help").setTabListener(tabListener);
                mActionBar.addTab(tab);




  FragmentA extends Fragment{

           public FragmentA(){            
          }
          @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view =inflater.inflate(R.layout.fragment_image, container, false);


        return view;
    }
    public void getData(String s)
{   // your implementation
    }
}
sandy
  • 203
  • 1
  • 7
0

hey please pass the value when the fragment instance is created ,please take a look on below codes I just passwed an list once the fragment is loaded via viewpager

private void setupViewPager(ViewPager viewPager, List<AccountDetail> listAc) {
        Homepage.ViewPagerAdapter adapter = new Homepage.ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(new AllFragment("", listAc), "All");
        adapter.addFrag(new AllFragment("1", listAc), "Credit");
        adapter.addFrag(new AllFragment("2", listAc), "Debit");
        viewPager.setAdapter(adapter);
    }

And then write a constructor in your fragment be like this

 public AFragment(String text, List<AccountDetail> manufactist) {
        mText = text;
        manufactureListAcoonut = manufactist;
    }
swaroop
  • 294
  • 1
  • 2
  • 13