1.Activity->Fragment
使用Bundle將數(shù)據(jù)通過setArgument方法傳入實例化的Fragment幻赚,在Fragment啟動時將其傳入靶橱,在Fragment中調(diào)用getArgument方法獲得Bundle,再從Bundle中提取數(shù)據(jù)
String text = editext.getText().toString();
MyFragment5 fragment5 = new MyFragment5();
Bundle bundle = new Bundle();
bundle.putString("name", text);
fragment5.setArguments(bundle);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction beginTransaction = fragmentManager
.beginTransaction();
beginTransaction.add(R.id.layout, fragment5, "fragment5");
beginTransaction.commit();
2.Fragment->Activity
在自定義Fragment中定義接口营袜,在接口中定義回傳數(shù)據(jù)的函數(shù)撒顿,將想要回傳的數(shù)值的數(shù)據(jù)類型作為參數(shù)
Activity繼承該接口,并重寫回傳數(shù)據(jù)函數(shù)荚板,獲得Activity所請求的數(shù)據(jù)并對其操作
在Fragment的OnCreateView方法中調(diào)用該回傳數(shù)據(jù)函數(shù)凤壁,將希望傳回Activity的數(shù)據(jù)作為參數(shù)寫入回傳數(shù)據(jù)函數(shù),該回傳數(shù)據(jù)函數(shù)回調(diào)Activity中重寫的回傳數(shù)據(jù)函數(shù)跪另,對Activity進行操作
——即:在Fragment中定義回調(diào)接口拧抖,在包含F(xiàn)ragment的Activity中實現(xiàn)接口,F(xiàn)ragment可調(diào)用該回調(diào)方法將數(shù)據(jù)回傳給Activity
public class MyFragment5 extends Fragment{
private String code="have received!";
public MyListener listener;
//定義MyListener接口
public interface MyListener
{
public void refunc(String code);
}
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
listener=(MyListener) activity;
super.onAttach(activity);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment2, container, false);
TextView tv = (TextView) view.findViewById(R.id.text);
String text=getArguments().get("name").toString();
tv.setText(text);
Toast.makeText(getActivity(), "已成功接收到"+text, Toast.LENGTH_SHORT).show();
Toast.makeText(getActivity(), "向Activity發(fā)送"+code, Toast.LENGTH_SHORT).show();
//通過接口的方法回調(diào)Activity中重寫的方法免绿,向Activity傳遞數(shù)值
listener.refunc(code);
return view;
}
}
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main4);
editext = (EditText) findViewById(R.id.editText);
send = (Button) findViewById(R.id.send);
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String text = editext.getText().toString();
//通過Bundle將數(shù)據(jù)傳入fragment
MyFragment5 fragment5 = new MyFragment5();
Bundle bundle = new Bundle();
bundle.putString("name", text);
fragment5.setArguments(bundle);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction beginTransaction = fragmentManager
.beginTransaction();
beginTransaction.add(R.id.layout, fragment5, "fragment5");
beginTransaction.commit();
Toast.makeText(MainActivity4.this, "向Fragment發(fā)送數(shù)據(jù)" + text,
Toast.LENGTH_SHORT).show();
}
});
FragmentManager fragmentManager = getFragmentManager();
Fragment findFragmentById = fragmentManager.findFragmentById(R.id.frag);
MyFragment frag=(MyFragment) findFragmentById;
frag.setAaa("fragment靜態(tài)傳值");
}
@Override
public void refunc(String code) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity4.this, "已成功接收到" + code ,
Toast.LENGTH_SHORT).show();
}
}
3.生命周期
4.Fragment事物管理
FragmentManager類:
通過finfFragmentById或findFragmentByTag獲取對應(yīng)的Fragment
調(diào)用popBackStack將Fragment從后臺棧彈出(模擬用戶按下back)
FragmentTransaction類:
添加唧席,刪除,替換Fragment。在操作確定后通過commit方法提交事務(wù)
可在調(diào)用commit之前通過addToBackStack將事務(wù)加入back棧淌哟,允許用戶回退到前一個Fragment狀態(tài)