更换用片段本身不显示任何东西 [英] Replacing a Fragment with itself does not show anything
问题描述
我试图决定并显示一个片段活动的 onResume 方式,但万一pviously增加了$ P $片段再次选择,那么活动变为空白。
I'm trying to decide and show a fragment in activity's onResume method, but in case a previously added fragment is chosen again, then the activity goes blank.
样code(带一个片段):
Sample code (with one fragment):
@Override
protected void onResume(){
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
trans.replace(R.id.myLayout, fragA);
trans.commit();
getSupportFragmentManager().executePendingTransactions();
}
使用上述code,当第一次创建活动中,它显示的弗拉加正确的,但如果我preSS <大骨节病>主页键骨节病>和然后切换回我的活动(为了挑起的 onResume 再次),这一切都变空白(好像弗拉加被删除)。
With above code, when the activity is created for the first time, it shows fragA correctly, but in case I press Home Key and then switch back to my activity (in order to provoke onResume again), it all goes blank (seems like fragA is removed).
时更换pviously添加片段的$ P $删除自身?或如何不松动片段,如果它是由自己更换?
Is replacing a previously added fragment removes itself? or how not to loose a fragment if it is replaced by itself?
推荐答案
您不能用自身替换片段。的更换上半年就是在那个ID的去除previous片段。一旦一个片段被去除它不再能加入或通过片段管理器(这样的替换将无法正常工作的附加的部分)使用。
You can't replace a fragment with itself. The first half of a replace is a removal of the previous fragment at that id. Once a fragment is removed it can no longer be added or used by the fragment manager (so the add portion of the replace will not work properly).
根据您的使用情况,您有两种选择:
Depending on your use case, you have two options:
- 创建一个新片段,而不是重用现有的实例
- 使用其他一些方法,看看是否有必要的,以取代片段
最后,你可能并不需要调用executePendingTransactions。
Finally, you probably don't need to call executePendingTransactions.
这篇关于更换用片段本身不显示任何东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!