Activity管理Fragment主要依赖于FragmentManager
FragmentManager主要有一下几个功能:
1、使用findFragmentById()或findFragmentByTag()方法来获取指定Fragment。
2、调用popBackStack()方法将Fragment从后台栈中弹出(模拟用户按下BACK按键)。
3、调用addOnBackStackChangeListener()注册一个监听器,用于监听后台栈的变化。
**如果需要添加、删除、替换Fragment,则需要借助FragmentTransaction对象,FragmentTransaction代表Activity对Fragment执行多个改变。
提示:FragmentTransaction也被翻译成Fragment事务。与数据库事务类似的是,数据库事务代表了底层数组的多个更新操作;而Fragment事务代表了Activity对Fragment执行多个改变操作。
可以通过FragmentManager来获得FragmentTransaction:
FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
每个FragmentTransaction可以包含多个对Fragment的修改,比如包含调用了多个add()、remove()、和replace()操作,最后调用commit()方法提交事务。
Maintivity:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
LeftFragment:
public class LeftFragment extends Fragment{
private Button btn_1;
private Button btn_2;
private Button btn_3;
private FragmentTransaction fragmentTransaction;
private FragmentManager fragmentManager;
private RightFragment rightFragment;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fragmentManager=getFragmentManager();
fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.rightfragment, new RightFragment());
fragmentTransaction.commit();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.left_fragment, container,false);
btn_1=(Button) view.findViewById(R.id.btn_1);
btn_2=(Button) view.findViewById(R.id.btn_2);
btn_1.setOnClickListener(listener);
btn_2.setOnClickListener(listener);
return view;
}
@Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
private View.OnClickListener listener=new View.OnClickListener() {
public void onClick(View v) {
fragmentTransaction=fragmentManager.beginTransaction();
switch (v.getId()) {
case R.id.btn_1:
fragmentTransaction.replace(R.id.rightfragment, new RightFragment1());
break;
case R.id.btn_2:
fragmentTransaction.replace(R.id.rightfragment, new RightFragment2());
break;
default:
break;
}
fragmentTransaction.commit();
}
};
}
RightFragment:
public class RightFragment extends Fragment{
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.right_fragment, container, false);
return view;
}
}
RightFragment1:
public class RightFragment1 extends Fragment{
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.right_fragment1, container, false);
return view;
}
}
RightFragment2:
public class RightFragment2 extends Fragment{
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.right_fragment2, container, false);
return view;
}
}