Transferring data between fragments in the same activity

I work in a project with Activity, which hosts many fragments. Now I need to share some data (integers, strings, arraylist) between these fragments.

The first time I used static fields, but I think this is a bad way, then I found this solution .

but in my case there is no button to click. I just need to move between fragments is there an easy way to make data exchange between fragments

+6
source share
5 answers

I believe that the best solution for you is the presence of variables within the main activity and access to them from fragments. I mean, if you need to do SAME EVERYTHING, you can write the code inside the action and just call the method you need.

For this you need to use the interface

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

Then realize it within your activities

 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 add activity in ALL of your snippets:

 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 the get and se methods you created

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

+5
source

You have several options:

Parameters 1: If you need general information between fragments, you can use SharedPreferences to store information. From my point of view, this is a bad idea, because SharedPreferences. This is an example:

 SharedPreferences settings = context.getSharedPreferences("Preferences", Context.MODE_PRIVATE); SharedPreferences.Editor editor = settings.edit(); editor.putString("string1", var); editor.putBoolean("bool1", var); ... editor.commit(); 

Option 2: if you created a fragment, you can create a collection method to save some king of information in the fragment. For example, if you want to pass an array of strings, you can create a var array in a fragment (using the setter method). Later, when you created this fragment, you can use the setter method to pass this array to the fragment.

+1
source

The easiest way to do this is to use the Bundle . You are doing 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 to find data from one fragment to another. Hope this helps someone.

+1
source

You can create an application class (I mean the extension of the application), and then take some variable that you need, make getter and setter method. set the value of a variable and get the value when and where you need it. if you want, I can give you a demo code.

0
source

1. Use BroadCastReceiver (a bit more complicated)

2. Use SharePreference

3.save data in MyApplication extend application

-one
source

All Articles