如何将片段信息发送到 activity 并在 android 中反向发送

How to send information of fragment to activity and In reverse in android

如何将 activity 的信息发送到片段以及将片段的信息发送到 activity 在 android?

你的问题很不好而且不清楚!请在提问之前先阅读 this link 然后提问。

但是要在 Activity 中使用来自 Fragment 的自定义方法,您可以使用我下面的示例。

首先在你的activity中写下你的方法:

public void showMessage(){
    Toast.makeText(context, "YOUR MESSAGE", Toast.LENGTH_SHORT).show();
}

然后,您应该在片段中创建 activity 的实例:

public class YOUR_FRAGMENT_NAME extends Fragment {
   private YOUR_ACTIIVTY_NAME yourActivity;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_layout, container, false);

        //Initialize
        mainActivity = (MainActivity) getActivity();

        //Call method 
        yourActivity.showMessage();

    return view;
}

希望对你有帮助