fragment(2、动态创建)

frament可以由自动添加,主要代码如下:

FragmentManager fm = getSupportFragmentManager();  

FragmentTransaction transaction = fm.beginTransaction();  

mDemo = new DemoFragment();

transaction.replace(R.id.fragment_content, mDemo);

transaction.commit();

同时可以对每次fragment的操作增加回退栈,增加后back键的操作将首先处理fragment的回退,如下:

FragmentManager fm = getSupportFragmentManager();  

FragmentTransaction transaction = fm.beginTransaction();  

mDemo = new DemoFragment();

transaction.replace(R.id.fragment_content, mDemo);//replace = remove + add

transaction.addToBackStack(null);

transaction.commit();

 

你可能感兴趣的:(Fragment)