展开全部
可以通过FragmentTransaction的attach和detach来实现。
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (mLastFragment != null) {
//detach上一次的fragment
fragmentTransaction.detach(mLastFragment);
}
//找到fragment,findFragmentByTag是自定义的方法
Fragment fragment = fragmentManager.findFragmentByTag(tabName);
if (fragment == null) {
//add
fragmentTransaction.add(R.id.layout_fragment_id, fragment, tabName);
} else {
//attach
fragmentTransaction.attach(fragment);
}
//保存这个fragment
mLastFragment = fragment;
//事务commit
fragmentTransaction.commitAllowingStateLoss();
fragmentManager.executePendingTransactions();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (mLastFragment != null) {
//detach上一次的fragment
fragmentTransaction.detach(mLastFragment);
}
//找到fragment,findFragmentByTag是自定义的方法
Fragment fragment = fragmentManager.findFragmentByTag(tabName);
if (fragment == null) {
//add
fragmentTransaction.add(R.id.layout_fragment_id, fragment, tabName);
} else {
//attach
fragmentTransaction.attach(fragment);
}
//保存这个fragment
mLastFragment = fragment;
//事务commit
fragmentTransaction.commitAllowingStateLoss();
fragmentManager.executePendingTransactions();
追问
还是不行额
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询