Pass Fragments Data to Fragments in another activity

3

I have a problem that my application has stopped me. I have an activity with several fragments of which I need to shoot the event of one of the menu buttons, collect their data and send them to another activity to show them in the fragments for this activity.

    
asked by Gabriel Betancourt 13.03.2016 в 05:34
source

3 answers

4

Simply send the data you have in the fragment through a bundle, when they are received by the second activity you can use them in their respective fragment.

Bundle bundle = new Bundle();
Intent intent = new Intent(getActivity(),SegundaActividad.class);
intent.putExtra("dato_bundle", bundle);
startActivity(intent);

in the second Activity, within your onCreate () method:

Bundle bundle = getIntent().getExtras();
String valorRecibido= getIntent().getStringExtra("dato_bundle");

Here receiving the data of the bundle, they can be used in the fragment that has your second activity.

    
answered by 13.03.2016 в 18:17
2

You can use:

  

putString (String key, Size value)

In the Origin Fragment before processing beginTransaction ()

Bundle args = new Bundle(); 
args.putString("email", email);
fragment.setArguments(args);

In the onCreateView of the destination Fragment you can get the / them and save them wherever you want.

String EMAIL = getArguments() != null ? getArguments().getString("email") : "[email protected]";

You can send more, as many as you need:

args.putString("email", email);
args.putString("nombre", nombre);
args.putString("edad", edad);
args.putString("sexo", sexo);

Of course you can also pass other types of data (String, int, float, etc)

See: link

    
answered by 14.03.2016 в 18:24
1

I also had that problem!

When you want to pass DATA between two Fragments, you must use the class Bundle (package in Spanish, in computer terminology) once created, you must add a .putString (Key, Value), .putInt ( Key, Value) ...

And in the other Fragment where you receive the data, you make a Bundle b = getArguments(); and then if it is an int, for example you make a b.getInt(Key,valorPorDefecto)

    
answered by 14.03.2016 в 21:21