Как мне перейти от одного фрагмента к другому? - PullRequest
0 голосов
/ 08 июня 2018

Я хочу перенести некоторые данные, такие как «editText1Double» и «sum», в другой фрагмент (SecondFragment), который будет использовать эти данные.Как бы я это сделал?

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.first_fragment, container, false);
    editText1 = (EditText) v.findViewById(R.id.editText1);
    editText2 = (EditText) v.findViewById(R.id.editText2);
    editText3 = (EditText) v.findViewById(R.id.editText3);
    textView = (TextView) v.findViewById(R.id.textView);


    calculateButton = (Button) v.findViewById(R.id.calculateButton);
    calculateButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            editText1Double = Double.parseDouble(editText1.getText().toString());
            editText2Double = Double.parseDouble(editText2.getText().toString());
            editText3Double = Double.parseDouble(editText3.getText().toString());

            sum = editText1Double + editText2Double + editText3Double;
        }
    });

Ответы [ 3 ]

0 голосов
/ 08 июня 2018

Отправка данных из фрагмента_A

FragmentManager fM = getActivity().getSupportFragmentManager();
FragmentTransaction fT = fM.beginTransaction();
Fragment_B fragment = new Fragment_B();//receiving fragment
Bundle bundle = new Bundle();
bundle.putDouble("sum_key", sum);
fragment.setArguments(bundle);
fT.add(R.id.container, fragment);
fT.addToBackStack(null);
fT.commit();

Получение данных по фрагменту_B

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    Bundle bundle = getArguments();
        Double sum = bundle.getDouble("sum_key");
}
0 голосов
/ 08 июня 2018

1) Используя Bundle:

FragmentManager fM = getActivity().getSupportFragmentManager();
FragmentTransaction fT = fM.beginTransaction();
Fragment_B fragment = new Fragment_B();//receiving fragment
Bundle bundle = new Bundle();
bundle.putDouble("key", sum);
fragment.setArguments(bundle);
fT.add(R.id.container, fragment);
fT.addToBackStack(null);
fT.commit();

и получите как

Bundle bundle = getArguments();
Double sum = bundle.getDouble("key");

2) Используя Construcor:

FragmentManager fM = getActivity().getSupportFragmentManager();
FragmentTransaction fT = fM.beginTransaction();
Fragment_B fragment = new Fragment_B(sum);//receiving fragment
fT.add(R.id.container, fragment);
fT.addToBackStack(null);
fT.commit();

и получить как

public class Fragment_B extends Fragment{
private double sum;
 public Fragment_B (double sum) {
        this.sum= sum;
    }
 public Fragment_B () {

 }}
0 голосов
/ 08 июня 2018

Передайте данные, используя

fragment = new HomeFragment();
                Bundle bundle = new Bundle();
            bundle.putDouble("double", 1.1);
                fragment.setArguments(bundle);
                getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment, "HomeFragment").addToBackStack(null).commit();

И получите данные в другом фрагменте, используя

Bundle bundle = getArguments();
double d = bundle.getDouble("double")

Надеюсь, что у вас все хорошо.

...