How to Pass Data 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 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

Pass data between fragments and view pager2

ok you want to call the api only once , that is when the activity is created right ?

ok for that initialize a int variable and set the value to 0;

int a=0;

then use condition to call your api

if(a==0)
{
//Your code To call Api
a=1;
}

So Here After U call Your Api Once "a is set to 1" which does not satisfy the condition and it does not call the api for the second time ...

but "a=0" when the your class or activity is created or called ..the api is also called

This Solution Is Given , Keeping That The Activity Is Not Recalled Or Recreated Unnecessarily ( Or The Activity Is Not Recalled/Recreated On Changing The Fragment )

How to pass data between fragments using ViewModel(s)?

Try making the val as backing field

        val post
get() = leadViewModel.state.selectedPost.value


Related Topics



Leave a reply



Submit