繁体   English   中英

如何将数据从片段传递到另一个片段?

[英]How to pass data from fragment to another fragment?

我曾多次尝试将数据从一个片段发送到另一个片段,但它从来没有奏效。 我已经使用了“Intent”和“Bundle”,但没有任何效果。 他们说要用BroadCast Receiver,但网上资料很少。 谁能给我一个例子?

分片通信中没有intent传递的概念,分片只接受arguments代替。 这是一个使用bundle进行片段通信的简单示例:

  YourReceiverFragment newFragment = new YourReceiverFragment();
  Bundle args = new Bundle();
  args.putString("key1", data1);
  args.putString("key2", data1);
  newFragment.setArguments(args);

并从onCreateView()中的另一个(接收器)片段中获取它,例如:

String value1 = getArguments().getString("key1");
// and so on

但是,片段到片段通信的另一个好习惯是通过Activity使用interfaces ,例如:

发件人片段

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

活动

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

接收器片段

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

让我知道它是否有帮助。

除非您使用视图模型,否则片段不会直接通信。 这是一个可以帮助您开始使用片段的指南

https://github.com/codepath/android_guides/wiki/Creating-and-Using-Fragments

您可以使用 viewmodel 在片段之间共享数据,您可以在 repo 中查看详细信息: 在此处输入链接描述

可以使用 bundle 在片段之间传递数据

你必须在你试图改变片段的地方写这个

Bundle bundle = new Bundle();
bundle.putInt("key_name1", value1);
bundle.putString("key_name2", value2);


FragmentB fragmentB = new FragmentB();
fragmentB.setArguments(bundle);

在 FragmentB 中,您可以接收此数据(您可以在片段的 onViewCreated() 生命周期中编写此数据):

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",""); 
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM