先說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);
}
}
至於用廣播去專遞消息,這裏就不明說了。
其他的更好的方法,謝謝大家分享一下。