导航抽屉中片段之间的通信 [英] Communication between fragment in Navigation Drawer

查看:341
本文介绍了导航抽屉中片段之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在单击按钮时将数据从片段A发送到NAVIGATION Drawer的片段B.我尝试了bundle和intent,但它们均无法正常工作.

I am trying to send data from Fragment A to Fragment B of NAVIGATION Drawer on Button click.I tried with bundle and intent but both of them are not working.

在片段A中,当我单击数据传递到另一个片段时,我有editText和按钮.

In Fragment A I have editText and button when I click the data is passed to another fragment.

在片段B中有textView,其中将显示editText数据,但是我没有办法在导航抽屉中的片段之间进行通信

In Fragment B there is textView where editText data is going to show but I am not getting a way to communicate between fragment in Navigation Drawer

推荐答案

从第一个片段启动片段

Bundle bundle = new Bundle();
bundle.putString("key", YOUR_EDITVIEW_TEXT);

Fragment fragment = new SECONDFragment();

if (arguments != null) {
    fragment.setArguments(arguments);
}

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.container, fragment);
ft.addToBackStack("");
ft.commit();

SecondFragment

private String mData;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getArguments() != null) {
        mData = getArguments().getString("key");
    }


}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.YourLayout, container, false);

    TextView text = (TextView) rootView.findViewById(R.id.yourTextView);
    text.setText(mData);
    return rootView;
}

这篇关于导航抽屉中片段之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆