Passing data from one fragment to another

后端 未结 4 1885
星月不相逢
星月不相逢 2020-12-06 20:25

I have an Activity with two fragments and I need to pass a string from FragmentA to FragmentB.

To pass the data, I have this in my FragmentA:

    Int         


        
4条回答
  •  遥遥无期
    2020-12-06 20:52

    You can do something like below,

     Fragment fr=new friendfragment();
     FragmentManager fm=getFragmentManager();
     android.app.FragmentTransaction ft=fm.beginTransaction();
     Bundle args = new Bundle();
     args.putString("CID", "your value");
     fr.setArguments(args);
     ft.replace(R.id.content_frame, fr);
     ft.commit(); 
    

    To receive the data do the following,

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        String strtext = getArguments().getString("CID");    
        return inflater.inflate(R.layout.fragment, container, false);
    }
    

提交回复
热议问题