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布局


    

activity_fragment布局



    

你可能感兴趣的:(Activity向Fragment传值)