片段的行为:FragmentTransaction ::代替()和反向backStack操作 [英] Fragments behaviour: FragmentTransaction::replace() and reverse backStack operation
问题描述
我把这种方法从AFrag往前走BFrag:
showFragment()
{
FragmentTransaction fragmentTransaction = mFragmentMgr.beginTransaction(); //添加片段到容器内容查看
fragmentTransaction.replace(R.id.operation_fragments_frame,mBFrag,mBFrag.getTag()); //添加淡入淡出效果
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); //保持在后面堆栈中的事务,因此将发生逆转时,后退按钮是pressed
fragmentTransaction.addToBackStack(NULL); //提交事务
fragmentTransaction.commit();
}
它显示了一个新的片段(BFrag),更换previous一(AFrag),并保持信息有关交易,因此它可以自动上后退按钮pressed扭转/百废待兴。 P>
在后退按钮pressed一切看起来不错,在previous片段所示(AFrag)。但是,当我再次去向前(AFrag - > BFrag)我得到了一个片段已经添加了异常
。没有反向/撤消删除操作的新片段(BFrag)?这是预期的行为?
这是奇怪,因为在此之后,我决定成立一个检查:
如果(mBFrag.isAdded())
{
fragmentTransaction.show(mBFrag);
}
其他
{
fragmentTransaction.replace(R.id.operation_fragments_frame,mBFrag,mBFrag.getTag());
}
和剧照,就进入else语句...我得到的异常。
这是什么我做错了吗?
任何有识之士THX。
你试图使用其它方法,如删除(),然后做一个插件()。或类似的东西吗?我在其他一些后看到,replace()方法并不总是正确运行。
I call this method to go forward from AFrag to BFrag:
showFragment()
{
FragmentTransaction fragmentTransaction = mFragmentMgr.beginTransaction();
// Add fragment to the container ContentView
fragmentTransaction.replace(R.id.operation_fragments_frame, mBFrag, mBFrag.getTag());
// Add FADE effect
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
// Keep the transaction in the back stack so it will be reversed when backbutton is pressed
fragmentTransaction.addToBackStack(null);
// Commit transaction
fragmentTransaction.commit();
}
It shows a new fragment (BFrag), replace the previous one (AFrag) and keep info about the transaction, so it can be reversed/undone automatically on back button pressed.
When back button is pressed everything looks fine, the previous fragment is shown (AFrag). But when I go forward again (AFrag -> BFrag) I got a "Fragment already added exception".
Didn't the reverse/undone operation remove the new fragment (BFrag)? Is this the expected behaviour?
That's weird because after this, I decided to set a check:
if(mBFrag.isAdded())
{
fragmentTransaction.show(mBFrag);
}
else
{
fragmentTransaction.replace(R.id.operation_fragments_frame, mBFrag, mBFrag.getTag());
}
and stills, it gets into the else statement... and I get the exception.
Any insight on what am I doing wrong, please?
Thx.
have you tried to use an other method, like remove(), then do an add(). or anything similar? I saw on some other post that the replace() method does not always behave correctly.
这篇关于片段的行为:FragmentTransaction ::代替()和反向backStack操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!