-1

I am beginner to android..I created two fragment with one activity in my app..in fragment one I added one ExpanadableListview and two editText..and one onclick button..for onclickbutton I am doing fragment replacement..if i click button..i want to pass all data to fragment2..reuseIt..I have someconfusion..I should use..bundle..or..interface concept..can anyone help me..

Below is fragment1.. replacement code..

@Override
public void onClick(View view) {
  FragmentTransaction ft;
  ft = fm.beginTransaction();
  Frag2New frag2New = new Frag2New();
  ft.replace(R.id.total_content, frag2New);
  ft.addToBackStack(null);
  ft.commit();
}
pruthvi raj
  • 9
  • 1
  • 3

5 Answers5

2

You can use Interface to pass data between fragments. Declare an interface:

public interface OnButtonPressListener {
public void onButtonPressed(String msg,int value,ArrayList<Object> arrData);

}

on LayoutOne.java fragment:

public class LayOutOne extends Fragment {

OnButtonPressListener buttonListener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
     ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_one, null);
      Button but=(Button)root.findViewById(R.id.button1);
      but.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            buttonListener.onButtonPressed("Message From First Fragment",1,ArrayList objct);
        }
    });
    return root;
}

 @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            buttonListener = (OnButtonPressListener) getActivity();
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement onButtonPressed");
        }
    }

on fragment 2

public class LayOutTwo extends Fragment implements OnButtonPressListener{
 ViewGroup root;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    root = (ViewGroup) inflater.inflate(R.layout.layout_two, null);
    return root;
}
@Override
public void onButtonPressed(String msg,int i,ArrayList<object> obj) {
    // TODO Auto-generated method stub
     //print the values or show them
}
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
1

You should use Bundle for sending data from one fragment to the second fragment. the usage is very simple

sending data from Frag1 to Frag2

FragmentTransaction ft;
ft = fm.beginTransaction();
Frag2 f2 = new Frag2();
Bundle b = new Bundle();
b.putString("UNIQUE_KEY","YOURDATA");

f2.setArguments(b);
ft.replace(R.id.total_content, frag2New);
ft.addToBackStack(null);
ft.commit();

receiving data within Frag2

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View view = inflater.inflate(...);
    Bundle b = getArguments();
    if(b!==null)
    {
        String data = b.getString("UNIQUE_KEY");
        //doing something with this data 
    }
 }

Basically you could also pass objects and other primitives(int/float/double/long) as well inside of the bundle

visionix visionix
  • 780
  • 1
  • 8
  • 18
0

in activity:

public class YourActivity{
    private List<String> mYourSharedList;

    public List<String> getMyList(){ 
        return mYourSharedList;
    }
}

in any fragment from this activity:

public class YourFragment extends Fragment {
    private List<String> mMyListInFragment;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         mMyListInFragment = ((YourActivity)getActivity()).getMyList();
    }
}

mMyListInFragment - will be pointer to list in activity, so if you will get list like this you will work with the same list in all your fragments.

Stepan Maksymov
  • 2,618
  • 19
  • 31
0

The easiest way I found to send data between different components is this library. It's quite easy to set up and start using, and it'll save you loads of time, cause not only that you can send strings, integers as you can with bundles, you can also send your own custom objects.

Vucko
  • 7,371
  • 2
  • 27
  • 45
0

There are many ways to share data between fragments it depends on your need there is sample app try this hope it will solve your issue. https://developer.android.com/training/basics/fragments/communicating.html