fragment回退栈

https://blog.csdn.net/zhiyuan0932/article/details/52593039


方法介绍

addToBackStack(tag); 将Fragment添加到回退栈中,tag可以为空

popBackStack(); 清除回退栈中栈顶的Fragment

popBackStack(String tag, int i ); 

如果i=0,回退到该tag所对应的Fragment层

如果i=FragmentManager.POP_BACK_STACK_INCLUSIVE,回退到该tag所对应的Fragment的上一层

popBackStackImmediate 立即清除回退栈中栈顶Fragment

getBackStackEntryCount(); 获取回退栈中Fragment的个数

getBackStackEntryAt(int index) 获取回退栈中该索引值下的Fragment


Fragment2 f2 =newFragment2();

FragmentTransaction tx = getFragmentManager().beginTransaction();

tx.replace(R.id.fl, f2);//将当前的事务添加到了回退栈

tx.addToBackStack(null);

tx.commit();


static MyFragment newInstance(String s){

        MyFragment myFragment = new MyFragment();

        Bundle bundle = new Bundle();

        bundle.putString("DATA",s);

        myFragment.setArguments(bundle);

        return myFragment;

    }

//同样,在onCreatView中直接获取这个值

@Override  

  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.layout_fragment,container,false);

        Bundle bundle = getArguments();

        String data = bundle.getString("DATA");

        tv = (TextView) view.findViewById(R.id.id_fm_tv);

        if(data != null){

            tv.setText(data);

        }

        return view;

    }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在Android开发之Fragment动态使用一文中,说到replace方法切换Fragment,会造成Fragm...
    YungFan阅读 25,024评论 19 89
  • Fragment回退栈,加入到Fragment回退栈里面的fragment出栈后,处于栈顶的fragment也不会...
    jiting45阅读 774评论 0 0
  • 一、什么是回退栈 Activity切换时是通过栈的形式,不断压栈出栈,在Fragment的时候,如果你不是手动开启...
    张迅之乎者也阅读 9,000评论 2 8
  • 那么有可能存在一种情况,退出的Fragment是比较深,然后退出的时候需要把前面的都退出掉,比如A->B->C->...
    SDY_0656阅读 302评论 0 0
  • 首先要给家塾班坚持每天分享日志的大家的道歉,因为自己最近没有更好的协调时间的能力,所以这几天都没能很好的给大家的简...
    你好啊我叫露娜阅读 191评论 0 3