简体   繁体   English

如何在 Android 中从一个片段转移到另一个片段

[英]How to transfer from one fragment to another in Android

I want to transfer some data like "editText1Double" and "sum" to another fragment (SecondFragment) that will use those data.我想将一些数据(如“editText1Double”和“sum”)传输到将使用这些数据的另一个片段(SecondFragment)。 How would I do that?我该怎么做?

@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;
        }
    });

1) Using Bundle : 1)使用捆绑:

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();

and Receive as并接收为

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

2)Using Construcor: 2)使用构造函数:

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();

and Receive as并接收为

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

 }}

Pass data by using通过使用传递数据

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();

And get data in another Fragment using并使用另一个 Fragment 获取数据

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

Hope it works fine for you.希望它对你有用。

Sending data from fragment_A从 fragment_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();

Receiving data at fragment_B在 fragment_B 接收数据

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    Bundle bundle = getArguments();
        Double sum = bundle.getDouble("sum_key");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据从一个 Fragment 传输到另一个 Fragment? - How to transfer data from one Fragment to another? 如何将价值从一个片段转移到另一个片段 - How to transfer value from one fragment to another 如何将Arraylist从一个片段传输到另一个片段 - How to transfer Arraylist from one fragment to another 如何将数据从一个片段传输到另一个片段android - How can I transfer data from one fragment to another fragment android 如何将价值从一个片段转移到另一个片段 - How to transfer value from one fragment to another fragment 将数据从一个片段传输到另一个片段 - Transfer data from one fragment to another fragment 如何在Android Studio中将控制权从一个活动转移到另一个活动的片段? - How to transfer control from one activity to fragment of another activity in android studio? 如何通过活动将额外数据从一个片段传输到另一个片段 - how to transfer extra data from one fragment to another through activity 如何将值传输到从另一个片段接收到的值的recylerview? - How to transfer the value to recylerview of one which is received from another fragment? 如何通过同一活动将从一个片段的列表视图选择的字符串传输到另一个片段的autocompleteTextview - How to transfer string selected from listview of one fragment to autocompleteTextview of another fragment over the same activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM