在前几天的项目中遇到一个很纠结的问题,如题。
问题描述:点击原先Fragment上面的按钮,弹出新的Fragment,hide() 、replace()、 add()三个方法傻傻分不清楚。
然后再百度上面转了好久都没有发现有什么非常好的解决方法。
想到hide的时候用不能获取当前的Fragment ,最后查看源码的时候发现fFragment中有个findFragmentByTag
根据一开始的add_fragment的标签来获取Fragment栈中的Fragment
步骤:
1.创建带有 “artist” 的Fragment:
ft.add(R.id.fmly_main, f,"artist");
2.查找 “artist” 的Fragment:Fragment lastFragment = getFragmentManager().findFragmentByTag("artist");
3.隐藏当前的Fragment,创建新的Fragment:
getFragmentManager().beginTransaction()
.hide(lastFragment)
.replace(R.id.fmly_main, Fragment.instantiate(getActivity(), TrackMusiListFragment.class.getName(),mBundle))
.addToBackStack(null)
.commit();