1

In my project i am using a Actionbar with viewpager and has three tabs which are fragments there is one more fourth fragemnt,when i click on the button in first fragment i am replacing it with fourth frag, now here where i am facing issue when i click on the list item of fourth frag i need to pass the list data to the first tab (first fragment) and display it. I am using Bundle for sending data as in my fourth frag below

public class PlaceSearchFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.autosearchactivity, container, false);

        return rootView;
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parentView, View view, int position,
                    long arg3) {

                int product =lv.getId();
                Bundle bundle = new Bundle();
                bundle.putInt("y", product); //any string to be sent
                System.out.println("Bundle loaded  "+product);
                Fragment newFragment = new GetRideFragment();
                newFragment.setArguments(bundle);

                          Fragment productDetailFragment = new GetRideFragment();
                  FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
                 transaction.remove(PlaceSearchFragment.this);
                  transaction.replace(R.id.replaceautosearch, productDetailFragment,"yourfragment").commit();   

}           
});     
}

And in my first frag i am calling bundle as below

public class Myplace extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.autosearchactivity, container, false);

        return rootView;
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

  if ( b != null ){
            b = getArguments();
            int s = b.getInt("y");
            System.out.println("printinf bundle data==>"+s);
            getfrom.setText(""+s);
}}

Any help is appreciated.

teekib
  • 2,821
  • 10
  • 39
  • 75

1 Answers1

0

Just use fragment constructor

private int value;

public MyFragment(int value) {
    this.value=value;
}

and call this fragment like this

MyFragment fragment = new MyFragment(25);
FragmentManager fragmentManager = activity.getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
Vinil Chandran
  • 1,563
  • 1
  • 19
  • 33