问题描述
我用几个片段
s到动态加入活动
。一切工作正常,当我preSS后退按钮时,片段
就去到 backstack
。当我恢复
,它出现。但每次在恢复
,它重现片段
并调用 onCreateView
。我知道这是片段
生命周期中的正常现象。
I am using several fragment
s to be dynamically added into activity
. Everything works fine, when I press back-button, the fragment
s go to backstack
. And when I resume
it, it appears. But everytime on Resume
, it is recreating the fragment
and call onCreateView
. I know it is a normal behavior of the fragment
lifecycle.
这是我的 onCreateView
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.competitive_programming_exercise, container, false);
return rootView;
}
我想停止那些片段
- 从重建。我试着用 onSavedInstanstate
,但没有什么工作。我怎样才能实现这个目标?
I want to stop those fragment
s from recreating. I tried with onSavedInstanstate
but nothing is working. How can I accomplish that?
推荐答案
您不能被重新停止片段,很遗憾。你能做的最好的是删除片段在一个事务中,它已被恢复后,但被其显示之前。
You can't stop the fragment from being recreated, unfortunately. The best you can do is to remove the fragment in a transaction, after it has been restored but before it gets displayed.
如果你知道你将要删除的片段,立刻就可以减少通过简化的方法,如 onCreateView恢复片段的性能损失()
返回一个虚拟视图,而不是再次膨胀整个视图层次结构。
If you know you are going to remove the fragment immediately you can reduce the performance hit of restoring the fragment by simplifying methods such as onCreateView()
to return a dummy view, rather than inflating the whole view hierarchy again.
不幸的是棘手的部分是找到提交本次交易的最佳场所。据这篇文章不会有太多的安全的地方。或许你可以尝试在 FragmentActivity.onResumeFragments()
或可能 Fragment.onResume()
。
Unfortunately the tricky part is finding the best place to commit this transaction. According to this article there are not many safe places. Perhaps you can try inside FragmentActivity.onResumeFragments()
or possibly Fragment.onResume()
.
这篇关于从简历后,被重新停止片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!