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;

    }

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