I have tried several times to send data from one fragment to another and it never works. I already used "Intent" and "Bundle" but nothing works. They said to use BroadCast Receiver, but there is little information on the Internet. Can anyone give me an example?
There is no intent
passing concept in fragment communication, fragments only accepts the arguments
instead. Here is an simple example of fragment communication using bundle
:
YourReceiverFragment newFragment = new YourReceiverFragment();
Bundle args = new Bundle();
args.putString("key1", data1);
args.putString("key2", data1);
newFragment.setArguments(args);
And get it from another (receiver) fragment inside the onCreateView()
like:
String value1 = getArguments().getString("key1");
// and so on
But, Another good practice for fragment to fragment communication is to use interfaces
via Activity
like:
SenderFragment :
public class SenderFragment extends Fragment {
SenderFragmentListener mCommunication;
public SenderFragment() {}// Required empty public constructor
@Override
public void onAttach(Context context) {
super.onAttach(context);
mCommunication = (SenderFragmentListener) context;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_sender, container, false);
Button button = (Button) view.findViewById(R.id.btn_sender);
// on click button
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCommunication.messageFromSenderFragment("Hello Fragment i am Sender...");
}
});
return view;
}
//Interface for communication
public interface SenderFragmentListener {
void messageFromSenderFragment(String msg);
}
@Override
public void onDetach() {
super.onDetach();
mCommunication = null;
}
}
Activity :
public class MainActivity extends AppCompatActivity implements
SenderFragment.SenderFragmentListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void messageFromSenderFragment(String msg) {
FragmentManager manager = getSupportFragmentManager();
ReceiverFragment mReceiverFragment = (ReceiverFragment)manager.findFragmentById(R.id.frg_Receiver);
mReceiverFragment.youGotMsg(msg);
}
}
ReceiverFragment :
public class ReceiverFragment extends Fragment {
TextView tv_msg;
public ReceiverFragment(){} // Required empty public constructor
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_receiver, container, false);
tv_msg = (TextView) view.findViewById(R.id.tv_receiver);
return view;
}
//Receive message
public void youGotMsg(String msg) {
tv_msg.setText(msg);
}
}
Let me know if it helps.
Fragments don't communicate directly unless you use viewmodels. Here is a guide that can help you get started in working with fragments
https://github.com/codepath/android_guides/wiki/Creating-and-Using-Fragments
you can use viewmodel to share data between fragments, you can see detail in repo: enter link description here
Data can be passed between fragments using bundle
You have to write this in place where you are trying to change the fragment
Bundle bundle = new Bundle();
bundle.putInt("key_name1", value1);
bundle.putString("key_name2", value2);
FragmentB fragmentB = new FragmentB();
fragmentB.setArguments(bundle);
In FragmentB you can receive this data (you can write this in the onViewCreated() lifecycle of the fragment):
Bundle bundle = this.getArguments();
if (bundle != null) {
value1=bundle.getInt("key_name1", 0); //0 is the default value , you can change that
value2=bundle.getString("key_name2","");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.