How to Pass Values Between Fragments

How to pass values between Fragments

Step 1: To send data from a fragment to an activity

Intent intent = new Intent(getActivity().getBaseContext(),
TargetActivity.class);
intent.putExtra("message", message);
getActivity().startActivity(intent);

Step 2: To receive this data in an Activity:

Intent intent = getIntent();
String message = intent.getStringExtra("message");

Step 3: To send data from an activity to another activity, follow the normal approach

Intent intent = new Intent(MainActivity.this,
TargetActivity.class);
intent.putExtra("message", message);
startActivity(intent);

Step 4: To receive this data in an activity

  Intent intent = getIntent();
String message = intent.getStringExtra("message");

Step 5.: From an Activity you can send data to a Fragment with the intent as:

Bundle bundle = new Bundle();
bundle.putString("message", "From Activity");

// Set Fragmentclass Arguments
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);

And to receive a fragment in the Fragment onCreateView method:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("message");

return inflater.inflate(R.layout.fragment, container, false);
}

Pass data fragment to fragment in the same activity

We can use the Bundle to send the data from one fragment to the another fragment

SendingFragment

RecievingFragment fragment = new RecievingFragment ();
Bundle args = new Bundle();
args.putString("Key", "Value");
fragment.setArguments(args);

getFragmentManager().beginTransaction().add(R.id.container, fragment).commit();

RecievingFragment

In onCreateView of the new Fragment:

String value = getArguments().getString("YourKey");

passing data through navigation between fragments android studio

You can pass a bundle object as the second argument in .navigate() and access it in your fragment with getArguments().

final Bundle bundle = new Bundle();
bundle.putString("test", "Hello World!");
Navigation.findNavController(view).navigate(R.id.action_qrSession_to_addSession, bundle);
public class MyFragment extends Fragment {

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// the string you passed in .navigate()
final String text = getArguments().getString("test");
}
}

Pass data between fragments

Fragments should generally only communicate with their direct parent activity. Fragments communicate through their parent activity allowing the activity to manage the inputs and outputs of data from that fragment coordinating with other fragments or activities. Think of the Activity as the controller managing all interaction with each of the fragments contained within.

A few exceptions to this are dialog fragments presented from within another fragment or nested child fragments. Both of these cases are situations where a fragment has nested child fragments and that are therefore allowed to communicate upward to their parent (which is a fragment).

The important thing to keep in mind is that fragments should not directly communicate with each other and should generally only communicate with their parent activity. Fragments should be modular, standalone and reusable components. The fragments allow their parent activity to respond to intents and callbacks in most cases.

There are three ways a fragment and an activity can communicate:

  • Bundle - Activity can construct a fragment and set arguments
  • Methods - Activity can call methods on a fragment instance
  • Listener - Fragment can fire listener events on an activity via an interface

In other words, communication should generally follow these principles:

  • Activities can initialize fragments with data during construction
  • Activities can pass data to fragments using methods on the fragment instance
  • Fragments can communicate up to their parent activity using an interface and listeners
  • Fragments should pass data to other fragments only routed through their parent activity
  • Fragments can pass data to and from dialog fragments
  • Fragments can contain nested child fragments

Read more about Fragment and its communication at Creating and Using Fragments

How to transfer some data to another Fragment?

Use a Bundle. Here's an example:

Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putInt(key, value);
fragment.setArguments(bundle);

Bundle has put methods for lots of data types. See this

Then in your Fragment, retrieve the data (e.g. in onCreate() method) with:

Bundle bundle = this.getArguments();
if (bundle != null) {
int myInt = bundle.getInt(key, defaultValue);
}


Related Topics



Leave a reply



Submit