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

时间:2019-09-23 21:11:23

标签: android android-intent broadcastreceiver bundle

我已经尝试过几次将数据从一个片段发送到另一个片段,但它永远无法正常工作。我已经使用了“ Intent”和“ Bundle”,但是没有任何效果。他们说使用BroadCast接收器,但是Internet上的信息很少。谁能给我一个例子吗?

4 个答案:

答案 0 :(得分:0)

片段通信中没有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

但是,片段到片段通信的另一种好的做法是通过interfaces使用Activity,例如:

SenderFragment

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

ReceiverFragment

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

让我知道是否有帮助。

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

您可以使用viewmodel在片段之间共享数据,可以在repo中查看详细信息: enter link description here

答案 3 :(得分:0)

可以使用捆绑软件在片段之间传递数据

您必须在尝试更改片段的地方写上这个

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