Fragment与Activity的联系

一、Fragment与Activity的生命周期

Fragment生命周期:

Fragment与Activity的联系_第1张图片
Fragment生命周期.jpg

二、Fragment的使用,实现底部菜单切换

(一)、先介绍两个使用Fragment的两个类:

1. FragmentManager manager

getSupportFragmentManager():用于管理FragmentManager

2. FragmentTransaction :用于管理事务的处理

FragmentTransacion ft=manager.beginTransaction();

  • 能对Fragment进行增加、删除、替换等操作。replace()会将fragment重新创建和销毁。
  • 每个事务可保存在Activity的后退栈中,如此用户能在fragment之间导航。
  • 切换Fragment时,Fragment被切换时,Fragment被销毁和重新创建;

(二)、实现底部菜单切换

有两种方式:

1. 第一种是使用FragmentTransaction的replace()方法

Activity布局:



    
    
        
    
    
        
    
        

2、实现的点击方法

public class MainActivity extends AppCompatActivity {
    NewsGoodsFragment newsGoodsFragment;
    BoutiqueFragment  boutiqueFragment;
    CategoryFragment categoryFragment;
    CartFragment cartFragment;
    MineFragment mineFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void onClick(View view){
        FragmentManager manager=getSupportFragmentManager();
        FragmentTransaction fragmentTransaction=manager.beginTransaction();
        switch (view.getId()){
            case R.id.tvNewGoods:
                if(newsGoodsFragment==null){
                    newsGoodsFragment=new NewsGoodsFragment();
                    fragmentTransaction.replace(R.id.fragment,newsGoodsFragment);
                    fragmentTransaction.commit();
                }else{
                    fragmentTransaction.replace(R.id.fragment,newsGoodsFragment);
                    fragmentTransaction.commit();
                }
                break;
            case R.id.tvBoutique:
                if(boutiqueFragment==null){
                    boutiqueFragment=new BoutiqueFragment();
                    fragmentTransaction.replace(R.id.fragment,boutiqueFragment);
                    fragmentTransaction.commit();
                }else{
                    fragmentTransaction.replace(R.id.fragment,boutiqueFragment);
                    fragmentTransaction.commit();
                }
                break;
            case R.id.tvCategory:
                if(categoryFragment==null){
                    categoryFragment=new CategoryFragment();
                    fragmentTransaction.replace(R.id.fragment,categoryFragment);
                    fragmentTransaction.commit();
                }else{
                    fragmentTransaction.replace(R.id.fragment,categoryFragment);
                    fragmentTransaction.commit();
                }
                break;
            case R.id.tvCart:
                if (cartFragment==null){
                    cartFragment=new CartFragment();
                    fragmentTransaction.replace(R.id.fragment,cartFragment);
                    fragmentTransaction.commit();
                }else{
                    fragmentTransaction.replace(R.id.fragment,cartFragment);
                    fragmentTransaction.commit();
                }
                break;
            case R.id.tvMine:
                if (mineFragment==null){
                    mineFragment=new MineFragment();
                    fragmentTransaction.replace(R.id.fragment,mineFragment);
                    fragmentTransaction.commit();
                }else{
                    fragmentTransaction.replace(R.id.fragment,mineFragment);
                    fragmentTransaction.commit();

                }
                break;
        }
    }
}


2. 第二种是先初始化fragment,将fragment添加到该Activity的FragmentManager管理器中,

再记录当前点击的位置进行切换fragment;

public class MainActivity extends AppCompatActivity {
    NewsGoodsFragment newsGoodsFragment;
    BoutiqueFragment boutiqueFragment;
    CategoryFragment categoryFragment;
    CartFragment cartFragment;
    MineFragment mineFragment;
    Fragment fragment[];
    int currentIndex = 0;
    int index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addFragment();
    }
    private void addFragment() {
        newsGoodsFragment = new NewsGoodsFragment();
        boutiqueFragment = new BoutiqueFragment();
        cartFragment = new CartFragment();
        categoryFragment = new CategoryFragment();
        mineFragment = new MineFragment();
        fragment = new Fragment[5];
        fragment[0] = newsGoodsFragment;
        fragment[1] = boutiqueFragment;
        fragment[2] = categoryFragment;
        fragment[3] = cartFragment;
        fragment[4] = mineFragment;
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.fragment, newsGoodsFragment);
        transaction.add(R.id.fragment, boutiqueFragment);
        transaction.add(R.id.fragment, categoryFragment);
        transaction.add(R.id.fragment, cartFragment);
        transaction.add(R.id.fragment, mineFragment);
        transaction.show(newsGoodsFragment);
        transaction.hide(boutiqueFragment);
        transaction.hide(cartFragment);
        transaction.hide(categoryFragment);
        transaction.hide(mineFragment);
        transaction.commit();
    }
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvNewGoods:
                index = 0;
                break;
            case R.id.tvBoutique:
                index = 1;
                break;
            case R.id.tvCategory:
                index = 2;
                break;
            case R.id.tvCart:
                index = 3;
                break;
            case R.id.tvMine:
                index = 4;
                break;
        }
        if (currentIndex != index) {
            setFragment();
        }
    }
    private void setFragment() {
        getSupportFragmentManager().beginTransaction().
                hide(fragment[currentIndex]).show(fragment[index]).commit();
        currentIndex = index;
    }
}

三、FragmentActivity
FragmentActivity用来解决android3.0之前没有fragment的API
通过继承 FragmentActivity,在Android3之前的手机中的Activity中就能使用Fragment
四、Fragment的startActivityForResult

  • 若调用Fragment.startActivityForActivity方法,则返回的结果首先被Fragment.onActivityResult方法处理,然后传递到宿主Activity的onActivityResult方法.
  • 若在Fragment中调用的是宿主Activity的startActivityForActivity,则返回的结果被苏浙Activity.onActivityResult()方法处理
    五、Fragment与Activity的通信

你可能感兴趣的:(Fragment与Activity的联系)