android中查看fragment中的重用
我正在尝试将我的视图状态保存在我的片段中,但我担心我会泄漏我的活动。这就是我正在做的事情:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state){
if(mView != null){
View oldParent = mView.getParent();
if(oldParent != container){
((ViewGroup)oldParent).removeView(mView);
}
return mView;
}
else{
mView = inflater.inflate(R.id.fragview, null)
return mView;
}
}
我很担心,因为我知道所有视图都持有一个上下文,但我不知道它是活动上下文还是应用程序上下文(如果从充气器充气)。也许使用 getActivity().getApplication() 实用地创建视图并设置其属性而不是使用 inflater 会是一个更好的主意。我将不胜感激任何对此的反馈。
谢谢!
编辑:已确认活动泄漏,尽管此代码效果很好,但不要这样做:*(
I am trying to save my View states in my fragment but I am concerned I make be leaking my Activity. Here is what I am doing:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state){
if(mView != null){
View oldParent = mView.getParent();
if(oldParent != container){
((ViewGroup)oldParent).removeView(mView);
}
return mView;
}
else{
mView = inflater.inflate(R.id.fragview, null)
return mView;
}
}
I am concerned because I know all Views hold onto a context and I don't know if it is the Activity context or Application context if inflated from the inflater. Perhaps it would be a better idea to pragmatically create the view and set its attributes using getActivity().getApplication() rather than use the inflater. I would appreciate any feedback on this.
Thanks!
EDIT: Confirmed Activity leak, although this code works great don't do it :*(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
onSaveInstanceState()
(在片段中)和onRetainConfigurationInstance()
(在活动中)在配置更改期间维护“视图状态”。我不太确定您可能指的其他“视图状态”。由于使用
Application
进行视图膨胀似乎效果不佳,因此您应该从Activity
进行膨胀。因此,视图将保存对填充它们的 Activity 的引用。Use
onSaveInstanceState()
(in the fragment) andonRetainConfigurationInstance()
(in the activity) for maintaining "View states" across configuration changes. I am not quite certain what other "View states" you might be referring to.Since using the
Application
for view inflation does not seem to work well, you should be inflating from theActivity
. And, hence, the views will hold a reference to the Activity that inflated them.@schwiz é 已经实现了类似的东西。
我在片段中使用了
setRetainInstance
。在片段布局上,我有一个 FrameLayout 占位符,我将 webView 放入其中。此 webView 是使用应用程序上下文在 Fragment 的 onCreate 上以编程方式创建的,并且我在
onCreateView()
中的膨胀布局上执行了addView(webView)
。在 onDestroyView 上,我只需从框架布局占位符中删除 webView 即可。
它工作得非常好,除非您尝试全屏播放视频。这不起作用,因为它需要一个活动上下文
@schwiz é have implemented something similar.
I use the
setRetainInstance
in the fragment. on the fragment layout I have a FrameLayout placeholder where I put my webView inside.This webView is created programmatically on the Fragment's onCreate using the Application Context, and I do a
addView(webView)
on the inflated layout inonCreateView()
.And on onDestroyView I simply remove the webView from my framelayout placeholder.
It work really well, unless you try to play a video in fullscreen. That doesn't work because it expects an Activity Context
为了保存并保留视图的状态,您只需使用 View.onSaveInstanceState () 和 View.onRestoreInstanceState (Parcelable state) 即可
它将帮助您独立于 Activity 或 Fragment 来处理保存和恢复视图状态。
有关详细信息,请参阅此答案(如何防止自定义视图在屏幕方向更改时丢失状态)
In order to save and retain view's state you can just use
View.onSaveInstanceState ()
andView.onRestoreInstanceState (Parcelable state)
It will help you to handle saving and restoring view state independantly from neither activity or fragment.
See this answer for more information about it (how to prevent custom views from losing state across screen orientation changes)