I have two Fragments called “gesamt” and “rennrad”. I want to send data from an EditText from gesamt to an EditText to rennrad by clicking a button(test).
Here is the part of code i tried it with in “gesamt” Fragment:
public class Gesamt extends Fragment {
Button test;
EditText kmrr;
test.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle result = new Bundle();
result.putString("kmrr", kmrr.getText().toString());
getParentFragmentManager().setFragmentResult("datakmrr", result);
}
});
Here is the code to receive the data in “rennrad” Fragment:
public class Rennrad extends Fragment {
TextView rrkmgesamtzahl;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.rennrad, container, false);
rrkmgesamtzahl = v.findViewById(R.id.rrkmgesamtzahl);
getParentFragmentManager().setFragmentResultListener("datakmrr", this, new FragmentResultListener() {
@Override
public void onFragmentResult(@NonNull String requestKey, @NonNull Bundle result) {
String data = result.getString("kmrr");
System.out.println(data);
rrkmgesamtzahl.setText(data);
}
});
return inflater.inflate(R.layout.rennrad, container);
}
}
I followed a video guide to put the data into a bundle, send it to the other Fragment to open it there and set the data to the TextView in the rennrad Fragment.
So, if i type “500” into the gesamt EditView, press the test button i want the 500 to be shown in the TextView on rennrad Fragment.
I also tried to debugg it and i know the data “500” is successfully transfered onto the new fragment but it is not shown.