How to send data from one Fragment to another?

0

 If we want to send data from one fragment to another then we will write code on Fragment java class.


First, create two fragments FirstFragment and SecondFragment.

The fragment is nothing without activity means the Fragment will be loaded on the main activity container.


Navigation from First Fragment to Second Fragment:-


Code of First Fragment:-


 public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_first, container, false);
btn = v.findViewById(R.id.btnfragmentclick);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// txtv.setText("Welcome in Fragment");
SecondFragment sf = new SecondFragment();
Bundle args = new Bundle();
args.putString("rno", "ssssssssss");
sf.setArguments(args);
getFragmentManager().beginTransaction().add(R.id.frgcontainer, sf).commit();
}
});
return v;
}
}



Code of Second Fragment:-

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_second, container, false);
String value = getArguments().getString("rno");

Toast.makeText(getContext(),value,Toast.LENGTH_LONG).show();

return v;
}
}



Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)