Activity与Fragment如何通信

概述:

Activity向Fragment传值在很多app中都有类似的使用,虽然现在的eventBus和RxBus都可以代替Activity向Fragment传值,但是这里还是要记录下,毕竟不是所有项目中都使用了EventBus或者RxBus。


核心:


发送数据:

//Activity传值,通过Bundle
Bundle bundle = new Bundle();
bundle.putString("MainActivity", "Hello,Fragment");
//首先有一个Fragment对象 调用这个对象的setArguments(bundle)传递数据
myFragment.setArguments(bundle);

//点击按钮接受Fragment传回来的值
btn_send.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    Bundle bundle = myFragment.getArguments();
    String result = bundle.getString("MyFragment");
    Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
    }
});

接收数据:

//点击按钮接收Activity传过来的值
btn_fragment.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    Bundle bundle = getArguments();
    String result = bundle.getString("MainActivity");
    Toast.makeText(getActivity(), result, Toast.LENGTH_LONG).show();
    }
});

//Fragment回传值给Activity
getArguments().putString("MyFragment", "Hello,Activity");

MainActivity.java:


public class MainActivity extends Activity {
    private Button btn_send;
    private MyFragment myFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_send = (Button)findViewById(R.id.button);

        FragmentManager manager = getFragmentManager();
        FragmentTransaction transation = manager.beginTransaction();
        myFragment = new MyFragment();
        transation.add(R.id.layout_container_fragment, myFragment);
        transation.commit();

        //Activity传值,通过Bundle
        Bundle bundle = new Bundle();
        bundle.putString("MainActivity", "Hello,Fragment");
        //首先有一个Fragment对象 调用这个对象的setArguments(bundle)传递数据
        myFragment.setArguments(bundle);

        //点击按钮接受Fragment传回来的值
        btn_send.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = myFragment.getArguments();
                String result = bundle.getString("MyFragment");
                Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
            }
        });
    }
}

MyFragment.java:


public class MyFragment extends Fragment{
    private Button btn_fragment;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_fragment, container, false);
        btn_fragment = (Button)view.findViewById(R.id.button_fragment);
        //点击按钮接收Activity传过来的值
        btn_fragment.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = getArguments();
                String result = bundle.getString("MainActivity");
                Toast.makeText(getActivity(), result, Toast.LENGTH_LONG).show();
            }
        });

        //Fragment回传值给Activity
        getArguments().putString("MyFragment", "Hello,Activity");
        return view;
    }
}

activity_main.java:


    


你可能感兴趣的:(项目总结)