Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing data between Fragments in the same Activity

Am working in a project with an Activity that host many fragments. Now I need to share some data (integers, strings, arraylist) between these fragments.

First time i used static fields but I think it's a bad way then I found this solution.

but in my case there is no button to click i have just to navigate between fragments is there any simple way to make sharing data between fragments

like image 492
Soufiane Avatar asked Jul 12 '16 14:07

Soufiane


2 Answers

I think the best solution for you is to have the variables inside the main activity and access them from fragments. I mean, if you have to do THE SAME in all fragments, you can write the code inside the activity and just call the method you need.

You need to use an interface for this purpose

public interface DataCommunication {
    public String getMyVariableX();
    public void setMyVariableX(String x);
    public int getMyVariableY();
    public void setMyVariableY(int y);
}

Then, implement it inside your activity

public class MainActivity extends Activity implements DataCommunication {

    private String x;
    private int y;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...   
    }

    ...

    @Override
    public String getMyVariableX(){
        return x;
    }

    @Override
    public void setMyVariableX(String x){
        //do whatever or just set
        this.x = x;
    }

    @Override
    public int getMyVariableY(){
        return y;
    }

    @Override
    public void setMyVariableY(int y){
        //do whatever or just set
        this.y = y;
    }

    ...

Then, attach the activity in ALL your fragments:

public class Fragment_1 extends Fragment{

    DataCommunication mCallback;

 @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (DataCommunication) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString()
                    + " must implement DataCommunication");
        }
    }

    ...

And finally, when you need to use a variable inside a fragment, just use get and se methods you've created

https://developer.android.com/training/basics/fragments/communicating.html

like image 137
adalpari Avatar answered Sep 17 '22 23:09

adalpari


The easiest way to do this is to use Bundle. You do something like this:

Fragment A:

Bundle b = new Bundle();
b.putString("Key", "YourValue");
b.putInt("YourKey", 1);

FragmentB fragB = new FragmentB();
fragB.setArguments(b); 
getFragmentManager().beginTransaction().replace(R.id.your_container, fragB);

Fragment B:

Bundle b = this.getArguments();
if(b != null){
   int i = b.getInt("YourKey");
   String s =b.getString("Key");
}

This is the easiest way that I have found to send data from one fragment to another. Hope it helps someone.

like image 43
BlackHatSamurai Avatar answered Sep 20 '22 23:09

BlackHatSamurai