先说Activity传递数据给Fragment
1.使用setArguments
1)在Activity中实例化Fragment对象
2)创建bundle对象
3)通过setArguments进行数据的传递
//Activity中的代码
MyFragment myFragment = new MyFragment();
Bundle bundle = new Bundle();
bundle.putString("data",values);//这里的values就是我们要传的值
myFragment.setArguments(bundle);
//Fragment中的接收代码
String str = (String)getArguments().get("data");
//下面这个也可以
Bundle bundle = getArguments();
String str = bundle.getString("data");
2.使用onAttach
1)在Activity中定义一个Public方法
//宿主activity中的getTitles()方法
public String getText(){
return "HelloWorld";
}
2)在Fragment里面的onAttach方法中通过强转成宿主Activity去操作方法
//Fragment中的onAttach方法
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
//通过强转成宿主activity,就可以获取到传递过来的数据
text = ((MainActivity) activity).getText();
}
再谈谈Fragment将值传给Activity
1.在Fragment中写一个回调接口
2.在activity中实现这个回调接口
3.在Fragment中onAttach 方法中得到activity中实现好的实例化接口对象
4.用接口的对象进行传值
Fragment代码如下:
public class MyFragment extends Fragment {
private Button button;
private EditText editText;
private MyFragment.CallBackValue callBackValue;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
/**
* fragment与activity产生关联是回调这个方法
*/
@Override
public void onAttach(Context context) {
// TODO Auto-generated method stub
super.onAttach(context);
//当前fragment从activity重写了回调接口 得到接口的实例化对象
callBackValue =(MyFragment.CallBackValue)getActivity();
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fff,null);
button = (Button)v.findViewById(R.id.button);
editText = (EditText)v.findViewById(R.id.editText);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String strValue = editText.getText().toString().trim();
callBackValue.SendMessageValue(strValue);
}
});
return v;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onStop() {
super.onStop();
}
//定义一个回调接口
public interface CallBackValue{
public void SendMessageValue(String strValue);
}
}
Activity代码如下:
public class MainActivity extends FragmentActivity implements MyFragment.CallBackValue {
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.text);
//在程序中加入Fragment
FragmentManager manager = getSupportFragmentManager();
//开启一个Fragment事务
FragmentTransaction transaction = manager.beginTransaction();
MyFragment myFragment = new MyFragment();
transaction.add(R.id.realtab_content, myFragment ,"000");
transaction.commit();
}
//要获取的值就是这个参数的值
@Override
public void SendMessageValue(String strValue) {
// TODO Auto-generated method stub
text.setText(strValue);
}
}
至于用广播去专递消息,这里就不明说了。
其他的更好的方法,谢谢大家分享一下。