Fragement (三)回退栈

天哥视频笔记

  • Fragment 回退栈

java 代码:AFragment.class

public class AFragment extends Fragment {
    private TextView mTvTitle;
    private Button btn_change;
    private Button btn_reset;
    private  BFragment bFragment;

    public static AFragment newInstance(String title){
        AFragment fragment=new AFragment();
        Bundle bundle=new Bundle();
        bundle.putString("title",title);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_a,container,false);
        Log.d("AFragment","-------onCreateView-------");
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mTvTitle=(TextView)view.findViewById(R.id.tv_title);
        btn_change = (Button)view.findViewById(R.id.btn_change);
        btn_reset = (Button) view.findViewById(R.id.btn_reset);
        btn_change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(bFragment == null){
                    bFragment=new BFragment();
                }
                //判断aFragment是否存在
                Fragment fragment=getFragmentManager().findFragmentByTag("a");
                if(fragment!=null){
                    //aFragment存在就先隐藏aFragment,然后再添加bFragment
                    getFragmentManager().beginTransaction().hide(fragment).add(R.id.fl_container,bFragment).addToBackStack(null).commitAllowingStateLoss();
                }else{
                    //在commit之前把fragment添加到回退栈
                    //replace=先remove+后add
                    getFragmentManager().beginTransaction().replace(R.id.fl_container,bFragment).addToBackStack(null).commitAllowingStateLoss();

                }

            }
        });
        btn_reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTvTitle.setText("我是新文字");
            }
        });

        if(getArguments()!=null){
            //获取参数
            mTvTitle.setText(getArguments().getString("title"));
        }

    }

}

关键代码1

1. 在container_activity中定义了aFragment的标签为a,所以这里通过findFragmentTag("a")获取aFragment

2. 判断afragment是否存在,若存在先调用hide方法隐藏,在添加bfragment,添加bFragment后需要调用addToBackStack(null)把fragment放到返回栈,否则返回时不会退到aFragment,而是退到container_activity

3.若不存在,直接调用replace方法更换bFragment,replace方法=先remove(aFragment)+add(bFragment)


Fragment fragment=getFragmentManager().findFragmentByTag("a");
if(fragment!=null){                 
    getFragmentManager().beginTransaction().hide(fragment).add(R.id.fl_container,bFragment).addToBackStack(null).commitAllowingStateLoss();
}else{
   //在commit之前把fragment添加到回退栈
   //replace=先remove+后add
    getFragmentManager().beginTransaction().replace(R.id.fl_container,bFragment).addToBackStack(null).commitAllowingStateLoss();
}

xml 代码:fragment_a.xml



    

java 代码:ContentActivity.class

public class ContainerActivity extends AppCompatActivity {
    private  AFragment aFragment;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_container);

        //R.id.fl_container:装在fragment的容器
        //aFragment:要加载的fragment
//        aFragment=new AFragment();
        //调用Fragment的一个静态方法newInstance
        aFragment=AFragment.newInstance("我是参数");
        //把AFragment添加到Activity中,记得调用commit
        //给fragmenta加一个标识"a",方便之后调用它
        getSupportFragmentManager().beginTransaction().add(R.id.fl_container,aFragment,"a").commitAllowingStateLoss();
    }

}

xml 代码:content_activity.xml




    


你可能感兴趣的:(Fragement (三)回退栈)