Activity向Fragment传值

1.核心代码:

发送数据

//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代码:

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
/**
 * Activity传值到Fragment
 * 并接收从Fragment返回的值
 * @author James
 *
 */
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代码:

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

/**
 *@author James
 *@date 2016年7月12日
 *@Version 1.0
 */
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布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:orientation="horizontal"
     >
    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="接收Fragmeng传回的值" />
    <LinearLayout
        android:id="@+id/layout_container_fragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#50ff"
        android:orientation="vertical"
        ></LinearLayout>
</LinearLayout>

activity_fragment布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <Button 
        android:id="@+id/button_fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="接受Activity传的值"
        />
</LinearLayout>

推荐阅读更多精彩内容